Skip to content Skip to sidebar Skip to footer

Document Is Not Defined In Nodejs

I am trying to build a website including HTML, CSS and JS. I have a HTML file and a JS file. I am trying to give the values of the HTML file to the JS file by using document.getEl

Solution 1:

Where are you running your js file?

The document object is only available in the browser, and not in the node environment. Try opening your html file in the browser - make sure you add your js script to the html file.

Have a look at this answer: Using Document object in nodejs

Solution 2:

JavaScript doesn't have a default document global. Browsers provide one, but your Node.js code doesn't run in a browser, it runs in the Node.js environment (e.g., as an application on your workstation, or as a server process, etc.).

To run the code you've shown, you'd include your .js file in a page by using a script tag in the HTML, typically at the end of body just before the closing </body> tag:

<scriptsrc="filename.js"></script></body>

Then you'd use a web server process (perhaps Node.js itself, perhaps using ExpressJS or Koa though you don't have to) or similar to provide that page in response to a request.

Post a Comment for "Document Is Not Defined In Nodejs"