javascript 终端中的节点(语法错误:意外标识符)js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31373973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node (SyntaxError: unexpected identifier ) js files in terminal
提问by Thomas Valadez
When I try running a javascript file through the terminal with node I get "SyntaxError: unexpected identifier"
当我尝试通过带有节点的终端运行 javascript 文件时,我收到“语法错误:意外标识符”
Here is my code saved as example.js
这是我保存为 example.js 的代码
console.log('hello world');
Here is what is happening in my terminal.
这是我的终端中发生的事情。
> 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)
Node responds to commands and code, but I cannot open files. Does anyone know what the issue may be?
Node 响应命令和代码,但我无法打开文件。有谁知道可能是什么问题?
回答by Michael Blankenship
Most people run that command outside of the node session itself.
大多数人在节点会话本身之外运行该命令。
> Thoms-MacBook-Pro:desktop thomvaladez$ node example.js
If you're already in the node session as you've done then--as t3dodson has suggested--you do a require at that point. Only you need to prepend that with a "./" so that it will find your file.
如果您已经在节点会话中,那么-正如 t3dodson 所建议的那样-此时您需要执行一个要求。只有你需要在它前面加上一个“./”,这样它才能找到你的文件。
> Thoms-MacBook-Pro:desktop thomvaladez$ node
> require ('./example.js')
> Hello World!
I'm guessing that "node" then in your version is the "Unexpected identifier".
我猜你的版本中的“节点”是“意外标识符”。