1

I am a self learner, I am having issues running my test files on Node.js. I am using the following command: "node ". I don't know what it means by document not being defined. In this case I am typing node test.js. Any help would be nice. I am faced with the the following errors:

PS C:\Users\buddys\Desktop\Projects> node test.js
C:\Users\buddys\Desktop\Projects\test.js:8
   document.write("Metri loves you too baby!");
   ^

ReferenceError: document is not defined
    at Object.<anonymous> (C:\Users\buddys\Desktop\Projects\test.js:8:4)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

Now when I run my index.html the JavaScript is writing to the webpage. Here is my index and .js file:

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Learning</title>

  </head>
  <body>
    <h1>JS for Metri</h1>
<p>blah blah blah</p>
<div class="content">

</div>

<!--place scripts at the bottom of the body unless big files. If it's a big files
Metri, make a separate .js file and link it to the HTML page by using src attribute. -->
<script src="test.js" charset="utf-8"></script>
  </body>
</html>

test.js

var youLikeMetri = true;
var age= 18;
var enter = 'You are permitted to be legally drunk';
var almost = 'You may legally smoke but not drink'
var exit= 'You are not permitted to be legally drunk';
//if statement if(condition){CODE BLOCK;}
 if (youLikeMetri){
   document.write("Metri loves you too baby!");
 }
//COMPARISON OPERATORS
//==EQUAL TO !=NOT EQUAL TO !==NOT IDENTICAL
//>=GREATER THAN OR EQUAL TO ||OR
//<=LESS THAN OR EQUAL TO &&AND
 if (age >= 21){
   document.write(enter);
 }
//elseif to create multiple conditions after first if.
else if(age <= 20){
  document.write(almost);
}

else if (age < 18){
  document.write(exit);
}
//while loops are continuous if the condition is met.
// while (condition){CODE BLOCK}
while (age <10) {
  console.log('You are less than 10');
  age++
  //adds one to age when age is > 10 it will continue down.
}
document.write('You are older than 10 now')
0

3 Answers 3

2

document is not part of standard JavaScript. It is an API provided by web browsers.

Node.js is not a web browser and does not provide a document object.

To write to STDOUT from Node.js you can use the process module.

process.stdout.write("Hello, world");
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so what I am trying to do is not possible? I was trying to run everything in my editor so I didn't have to bounce back between my browser and editor.
We should mention here that document.write is a bad method that is never used in good practices
@DemetriBethel process.stdout is the standard output stream. In most cases it is the command line interface you are using.
1

document belongs to DOM in web browsers since nodeJS is not a client-side javascript you can't access browsers DOM
But if you want to use nodeJS module on a client-side app you can use broserify

Comments

1

You got confuse between front end (your index.html that executing test.js) and the backend.

node js is for the backend. You can look at it as implementing the "web server" that sending both index.html and test.js in your case to the browser.

Working example for your case:

var express = require('express')
, http = require('http')
, path = require('path');

var app = express();

app.set('port', process.env.PORT || 3000);

app.use(express.errorHandler());
app.use(express.static(path.join(__dirname)));

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

save it as my_backend.js

Execute:

npm install express

Now run:

node my_backend.js

Make sure that your other files (index.html and test.js) saved in the same folder as my_backend.js.

Now, open your browser and type:

http://127.0.0.1:3000/

What it's doing is using the express framework to create a web server that listening on your local host port 3000 and just searching any resource that you are requesting from it in the same folder as my_backend.js.

For more information, please refer to any node.js + express tutorial.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.