[英]Node.js Error for Hello World example
I am new to node.js I just finished installing it on my windows machine. 我是node.js的新手,我刚刚将其安装在Windows机器上。 Actually i'm following a tutorial on tutorialspoint.
实际上,我正在遵循有关tutorialspoint的教程。 After the installation I was told to create a main.js file and put the following code in the file.
安装后,我被告知要创建一个main.js文件,并将以下代码放入文件中。
/* 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. 我通过输入$ node main.js使用Node.js解释器执行了main.js文件,但是出现以下错误。
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.
谢谢。
I think, you are run your node main.js
not from shell, but from node REPL
. 我认为,您不是从shell运行
node main.js
,而是从REPL
运行节点。
You don't need to run node
before. 您不需要先运行
node
。
$ cat main.js
console.log("Hello, World!")
$ node main.js
Hello, World!
Hm, you are on Windows. 嗯,您在Windows上。 Then you should do something like this in your cmd.exe:
然后,您应该在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. 注意:上面的
cat
和type
命令是多余的,仅用于文件内容演示。
Also, when you inside nodejs
REPL
, you can write javascript
code directly. 另外,当您使用
nodejs
REPL
,可以直接编写javascript
代码。
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. 您可以使用Windows 7的常规命令提示符从任何位置运行节点,但必须指定具有确切文件路径的js文件名。
for example : c:\\users[your name]>node d:\\projects\\js_files\\main.js 例如:c:\\ users [您的名字]>节点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. 如果您在执行任何操作之前已设置了path =“ c:\\ progam files \\ nodejs \\ bin”环境变量,那么它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.