0

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
João Silva
  • 71
  • 2
  • 8
  • 3
    What exactly do you expect `document.getElementById` to do in a non-browser environment with no DOM to scan? – CollinD Oct 27 '21 at 17:18
  • Please explain what you're trying to do. `document.getElementById` works if you have a document with elements that have IDs. None of that is the case with Node.js. So, if you explain *what* you want to do with this, we can probably tell you *how*. Right now, we can only say that it's incorrect. – VLAZ Oct 27 '21 at 17:20
  • Possibly related: [how to use document.getElementById() in Nodejs](https://stackoverflow.com/q/52256799) | [getElementById Function on Node.js](https://stackoverflow.com/q/44463507) | [getElementById doesn't work on a node](https://stackoverflow.com/q/3902671) – VLAZ Oct 27 '21 at 17:21

2 Answers2

2

node runs in a server environment, which doesn't have any document or window because it is not a browser environment.

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
congar
  • 229
  • 3
  • 8
1

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

JP-LISN
  • 143
  • 5