-3

I have this code here in VSCode:

const form = document.querySelector('form');

The error I am getting is:

C:\Program Files\nodejs\node.exe boot.js 
helloworld
c:\Users\frase\OneDrive\Desktop\html\boot.js:2
const form = document.querySelector('form');
             ^

ReferenceError: document is not defined
    at Object.<anonymous> (c:\Users\frase\OneDrive\Desktop\html\boot.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

Why is the document not working? The file name is referenced with the html file as <script src="boot.js"></script>

I have tried putting the script in the header and body tags.

Teemu
  • 22,918
  • 7
  • 53
  • 106
user218030
  • 107
  • 3
  • 12
  • 3
    You're running this in nodejs on the command line apparently, not in a browser using HTML…!? – deceze Feb 27 '20 at 08:55
  • Agree with @deceze, document (dom) object works for browser not from the terminal – Juhil Somaiya Feb 27 '20 at 08:56
  • Show your HTML pls – David Feb 27 '20 at 08:56
  • Node.js is just Chrome's console. Alone. Without a DOM, without HTML. You can only execute Javascript like `2+2` or `new Date()`, stuff like that. `document` is undefined because there is no document. No HTML to work on. – Jeremy Thille Feb 27 '20 at 08:59
  • @ImperialOsprey4 is it required to see html code ? – Juhil Somaiya Feb 27 '20 at 09:01
  • So I have to do scripts in the html file not an external js file? I have seen people do it outside of the html file. – user218030 Feb 27 '20 at 09:03
  • 1
    In a nutshell, Node.js is for _server-side_ Javascript. If you want to use JS the "regular" way ( = client-side), use a HTML file with a ` – Jeremy Thille Feb 27 '20 at 09:06
  • 1
    @user218030 No, you _can_ have them in external JS files and reference them with ` – Ivar Feb 27 '20 at 09:07
  • 1
    @user218030 yes, you can do js in external file, but we are trying to tell you is that you have to run it in the browser, not as a node application – David Feb 27 '20 at 09:09
  • @JeremyThille So basically things I want to update on the client side (what the client sees) is done via in the html file, and stuff I want to do server-side on a js file using node? – user218030 Feb 27 '20 at 09:10
  • 1
    You may want to see this: https://stackoverflow.com/a/13840431/476. Substitute "PHP" for "nodejs" here, the rest of the explanation is the same. – deceze Feb 27 '20 at 09:13

1 Answers1

0

document is available only when you are running javascript in a browser. It is not available by default.

Tech Solver
  • 513
  • 1
  • 5
  • 16
  • Sorry I don't understand. If I have a js file why doesn't the syntax work? I don't get what you mean. – user218030 Feb 27 '20 at 08:55
  • 1
    When you open an html file in a browser, the browser engine will created certain global javascript objects like document and window which can be later used by your javascript code. But if you are using javascript in some other node application then document object will not be present by default. – Tech Solver Feb 27 '20 at 08:56