Hello World示例的Node.js错误

时间:2015-11-26 04:19:51

标签: javascript node.js

我是node.js的新手 我刚刚在我的Windows机器上安装它。实际上我正在关注tutorialspoint的教程。安装完成后,我被告知要创建一个main.js文件并将以下代码放在文件中。

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

我通过键入$ 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)

请帮帮我。谢谢。

4 个答案:

答案 0 :(得分:6)

听起来像你在REPL(Read-Eval-Print-Loop)。尝试多次点击ctrl + c,看看是否退出命令提示符。然后尝试运行node main.js。你应该看到你想要的输出。

答案 1 :(得分:3)

我认为,您不是从shell运行node main.js,而是从节点REPL运行。

您之前无需运行node

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

嗯,你在Windows上。 然后你应该在你的cmd.exe中做这样的事情:

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

注意:上面的cattype命令是多余的,仅用于文件内容演示。

此外,当您在nodejs REPL内时,您可以直接编写javascript代码。
试试吧:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以使用Windows 7的正常命令提示符从任何地方运行节点,但您必须使用确切的文件路径指定js文件名。

例如:c:\ users [您的名字]&gt;节点d:\ projects \ js_files \ main.js

如果您在执行任何操作之前拥有set path =“c:\ progam files \ nodejs \ bin”环境变量,它将起作用。

相关问题