1

So, started learning node.js and tried the most basic thing in the world: A "Hello World" from the server.

the code is the following (copied from an actual book):

var http = require("http");
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(8888);

I hit the node.js executable, type node server.js and gives the famous SyntaxError: Unexpected identifier.

Tried looking for similar questions but couldn't find something that would help something as simple as what I'm trying to accomplish.

3
  • The code itself looks correct. Usually SyntaxErrors are due to typos, but I have copied and pasted your code and verified it works. Could be an environment issue? Commented Feb 29, 2016 at 3:57
  • 4
    You run node server.js from the command line, not inside the node REPL Commented Feb 29, 2016 at 3:58
  • Sorry, what's the difference between the command line and the node.js shell? Commented Feb 29, 2016 at 4:04

1 Answer 1

1

The node REPL aka node shell, the REPL provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out. Here is one good how-to-use-nodejs-repl

Whereas, the command line is used to run the node command like node server.js.

You run node server.js in command line.

Sign up to request clarification or add additional context in comments.

6 Comments

Seriously, I can't understand the difference between the console shell and the ''command line''. I thought it was the same thing.
@HiagoSerpa, the command line could run the command like node or other script file or the commands could be searched by Path environment. If you run node in command line, the Node REPL will come up, same as hit the node.js executable. which is Javascript interpreter, you can run javascript code in the REPL. Hope I make myself clearly...
I'm feeling mentally challenged here lol, So what should I do, apart from opening nodejs.exe and typing 'node server.js'?
We have all been there! You dont want to open node.exe! At least not now. In windows(8 or 10) you can right-click the windows button and choose command prompt. After that typing node server.js will run server.js using node. --- The difference is the same as if you were to open node.exe by double clicking versus dropping a file (server.js) on node.exe. (Except that the window will close really quickly, so opening the OS command line first is so we can see what is happening.) You can also shift-right-click a folder and choose "open a command window here".
@HiagoSerpa, ippi give you the right way to how to run. another way to click Run after click the window button, then input cmd in the run dialog, then OK, the command line will come up, and go to the right folder to run node server.js...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.