Home > Software engineering >  Getting "document is not defined" in JS
Getting "document is not defined" in JS

Time:10-28

I'm trying to build a simple Blackjack game, and everything was going well, until I started getting the error "document is not defined"... I'm using VS Code with Node, so I just write in the terminal "node index.js" and it always works, however for some reason it's not working now.

Here's the error:

let messageEl = document.getElementById("message-el");
                ^

ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\draven\Desktop\JAVASCRIPT\Blackjack\index.js:9:17)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

CodePudding user response:

node run server environment and this environment doesn't have any document or window cuz it is not a browser enviroment

CodePudding user response:

document is a web browser concept, it doesn't exist in the node runtime.

Check out this answer for the a great explination: Document in node

  • Related