Node.js Error for Hello World example

I'm new to node.js I just finished installing on my window machine. In fact, I am following a textbook textbook. After installation, I was told to create the main.js file and put the following code into the file.

/* Hello, World! program in node.js */ console.log("Hello, World!") 

I executed the main.js file using the node.js interpreter by typing $ node main.js, but I had the following errors.

 SyntaxError: Unexpected identifierat Object.exports.createScript (vm.js:24:10) at REPLServer.defaultEval (repl.js:221:25) at bound (domain.js:280:14) at REPLServer.runBound [as eval] (domain.js:293:12) at REPLServer.<anonymous> (repl.js:412:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:210:10) at REPLServer.Interface._line (readline.js:549:8) at REPLServer.Interface._ttyWrite (readline.js:826:14) 

Please help me. Thanks.

+5
source share
4 answers

It looks like you are in REPL (Read-Eval-Print-Loop). Try pressing ctrl + c couple of times and see if you get to the command line. Then try running node main.js You should see the desired result.

+6
source

I think you are using node main.js not from the shell, but from node REPL .

You do not need to run node before.

 $ cat main.js console.log("Hello, World!") $ node main.js Hello, World! 

Hm, you are on Windows. Then you should do something like this in your cmd.exe:

 c:\...> cd c:\projects\hello c:\...> type main.js console.log("Hello, World!") c:\...> node main.js Hello, World! 

Note. The commands above cat and type are redundant and just to demonstrate the contents of the file.


Also, if you are inside nodejs REPL , you can write javascript code directly.
Just try:

 > console.log('Hey'); 'Hey' undefined > require('./main.js'); Hello, World! undefined > exit Bye-bye 
+3
source

Do not run node before testing the program.

0
source

you can run node from anywhere using the regular windows 7 command prompt, but you must specify the js file name with the exact file path.

for example: c: \ users [your name]> node d: \ projects \ js_files \ main.js

it will work if you have the environment variable set path = "c: \ progam files \ nodejs \ bin" before doing anything.

0
source

Source: https://habr.com/ru/post/1236871/


All Articles