harry r
harry r

Reputation: 13

document not defined javascript error (works in Chrome console but not in integrated terminal of editor)

I am using Visual Studio Code and I like to test my console.logs or my javascript using the integrated terminal in VSC. I have node.js installed and I use the 'node .' command to test run my javascript. I get this error

console.log(document.getElementById("para").textContent); ^

ReferenceError: document is not defined

I am just testing some stuff. My html code, javascript code

However, when I test my code through the chrome console, it works for me. No document not defined error or anything. Can anyone assist me on this? Thanks !

Upvotes: 0

Views: 3075

Answers (1)

Harkat
Harkat

Reputation: 162

document Only exists in the browser. If you are trying to just run your Javascript directly without the accompanying browser, this will fail.

The same also goes for the window object, these are both only available in the actual browser environment. This is the reason this works in the chrome console, as you are on a webpage and have access to these objects.

Upvotes: 2

Related Questions