简体   繁体   中英

Node.js Error for Hello World example

I am new to node.js I just finished installing it on my windows machine. Actually i'm following a tutorial on tutorialspoint. After the installation I was told to create a main.js file and put the following code in the file.

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

I executed main.js file using 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 out. Thank you.

Sounds like you are in the REPL (Read-Eval-Print-Loop). Try to hit ctrl + c a couple of times and see if you exit out to the command prompt. THEN try running node main.js . You should see your desired output.

I think, you are run your node main.js not from shell, but from node REPL .

You don't 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: cat and type commands above are redundant and just for file content demonstration.


Also, when you 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

在测试程序之前不要运行node

you can run node from anywhere using normal command prompt of windows 7 but you have to specify the js file name with exact file path.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM