# Get empty nodelist when run with vs code

**URL:** <https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223>\
**Category:** General\
**Created:** [December 13, 2020, 2:47pm UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223 "2020-12-13T14:47:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![beerbluedevils](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/beerbluedevils/32/178578_2.png) [@beerbluedevils](https://forum.freecodecamp.org/u/beerbluedevils)\
**Post date:** [December 13, 2020, 2:47pm UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/1 "2020-12-13T14:47:16Z")

</div>

![empty nodelist](https://global.discourse-cdn.com/freecodecamp/original/3X/e/5/e5a9aca1cf9f3a9b14055b9683bcc21265070f75.png)

I try to run script from VS code , it always show empty nodelist but when I run with chrome console it works properly. Please give me some advice if I’m doing it wrong.

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [December 13, 2020, 4:12pm UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/2 "2020-12-13T16:12:33Z")

</div>

How are you running the script in the HTML, when does the code run? Is the DOM ready?

Try wrapping the code inside a [DOMContentLoaded](https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event) handler.

---

<div class="post-metadata">

**Author:** ![beerbluedevils](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/beerbluedevils/32/178578_2.png) [@beerbluedevils](https://forum.freecodecamp.org/u/beerbluedevils)\
**Post date:** [December 14, 2020, 1:46am UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/3 "2020-12-14T01:46:33Z")

</div>

```auto
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Javascript</title>
    <script src="app.js">
    </script>
</head>
<body>
 
</body>
</html>

```

I try to use DOM method to the webpage.  
This is how I linked js file to the HTML file(It’s in the same folder). When I run on local or on server (with vscode) it looks like js file doesn’t link on HTML file but when I write the same code on developer tool console it works fine.

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [December 14, 2020, 1:50am UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/4 "2020-12-14T01:50:19Z")

</div>

Hey @beerbluedevils,

Looks like you got the case of putting the JavaScript on the head. This will not work because the javascript will run BEFORE the DOM is loaded. What you need to do is put it at the end, before the `</body>` ends like this:

```auto
<body>

<!-- HTML -->

<script src="app.js"></script>
</body>

```

Another solution is to add the `defer` attribute inside the script tag, so it will run after the DOM is loaded. like this: `<script defer src="app.js"></script>`

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![beerbluedevils](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/beerbluedevils/32/178578_2.png) [@beerbluedevils](https://forum.freecodecamp.org/u/beerbluedevils)\
**Post date:** [December 14, 2020, 2:03am UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/5 "2020-12-14T02:03:17Z")

</div>

It works! Thank you very much. I have one more question.  
Does the place of the script matter? For example : if I want to run script after the

I need to put it after that not on the head.

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [December 14, 2020, 2:04am UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/6 "2020-12-14T02:04:39Z")

</div>

Yes, the position of the script does actually matter… Let’s say you want to run a function from a different file, you have to put the script that have the function on top of the script that you want to run the function in. That’s why when library is being used, it will often be on top of the main JavaScript file.

---

<div class="post-metadata">

**Author:** ![beerbluedevils](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/beerbluedevils/32/178578_2.png) [@beerbluedevils](https://forum.freecodecamp.org/u/beerbluedevils)\
**Post date:** [December 14, 2020, 2:11am UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/7 "2020-12-14T02:11:07Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:36pm UTC](https://forum.freecodecamp.org/t/get-empty-nodelist-when-run-with-vs-code/435223/8 "2021-06-01T18:36:25Z")

</div>

Closed on June 1, 2021, 6:36pm UTC


