2015-07-13 121 views
4

文件,当我尝试运行通过与节点的终端的JavaScript文件,我得到“语法错误:意外标识符”节点(语法错误:意外标识符)JS在终端


这里是我的代码保存为example.js

console.log('hello world');


这是在我的终端发生了什么。

> Thoms-MacBook-Pro:desktop thomvaladez$ node 
 
> console.log('hi'); 
 
hi 
 
undefined 
 
> node example.js 
 
SyntaxError: Unexpected identifier 
 
    at Object.exports.createScript (vm.js:44:10) 
 
    at REPLServer.defaultEval (repl.js:117:23) 
 
    at bound (domain.js:254:14) 
 
    at REPLServer.runBound [as eval] (domain.js:267:12) 
 
    at REPLServer.<anonymous> (repl.js:279:12) 
 
    at REPLServer.emit (events.js:107:17) 
 
    at REPLServer.Interface._onLine (readline.js:214:10) 
 
    at REPLServer.Interface._line (readline.js:553:8) 
 
    at REPLServer.Interface._ttyWrite (readline.js:830:14) 
 
    at ReadStream.onkeypress (readline.js:109:10)

节点响应命令和代码,但我无法打开文件。有谁知道这个问题可能是什么?

回答

5

大多数人在节点会话本身之外运行该命令。

> Thoms-MacBook-Pro:desktop thomvaladez$ node example.js 

如果你为你做那么在节点会话是已经 - 作为t3dodson已经建议 - 你需要一个在这一点上。只有你需要用“./”作为前缀才能找到你的文件。

> Thoms-MacBook-Pro:desktop thomvaladez$ node 
> require ('./example.js') 
> Hello World! 

我猜你的版本中的“节点”是“Unexpected identifier”。

相关问题