I'm still new to javascript and node.js and was trying to accomplish something but I am encountering the error ReferenceError: Document not defined
Given the following code in seperate files:
Index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Test Website</title>
<script type="text/javascript" src="script.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p id="para1"> Some text here</p>
</body>
</html>
And script.js:
function getParagraphContents() {
var paragraph = document.getElementById("para1");
var temp = paragraph.textContent;
console.log(temp);
}
So my problem being is I wanted to be able to run the command node script.js from the command line but keep getting an error message. I keep reading other answers and documentation but nothing seems to be the right answer. Any suggestions? P.S. I am using terminal on mac osx and have node and npm install correctly.
Thank you.
script.jsis designed to run in a browser and to operate on the current page's HTML. Runningnode script.jsdoes not really make any sense. While you can find libraries that will load an HTML document into node and give you a DOM representation of them that you can then run some browser-like scripts on, that isn't what you're attempting here as there is no document associated withscript.jsat all. It's just a script by itself. So runningnode script.jsis like half an app. You need to explain what you're trying to accomplish from node.