Hello World示例的Node.js错误

我刚接触node.js,刚刚在Windows机器上完成安装。 其实我正在关于tutorialspoint的教程。 安装完后,我被告知创build一个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) 

请帮我一下 谢谢。

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

我想,你不是从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 

在testing程序之前不要运行node

您可以使用Windows 7的常规命令提示符从任何地方运行节点,但是您必须指定具有精确文件path的js文件名。

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

如果在执行任何操作之前设置了path =“c:\ progam files \ nodejs \ bin”环境variables,它将会工作。