首页 > 解决方案 > 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 !

标签: javascripthtmlnode.jsdomvisual-studio-code

解决方案


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.


推荐阅读