【问题标题】:Node (SyntaxError: unexpected identifier ) js files in terminal终端中的节点(语法错误:意外标识符)js文件
【发布时间】:2015-10-01 03:54:30
【问题描述】:

当我尝试通过带有节点的终端运行 javascript 文件时,我得到“SyntaxError:意外标识符”


这是我保存为 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)

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

【问题讨论】:

    标签: javascript node.js terminal


    【解决方案1】:

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

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

    如果您已经在节点会话中,那么 - 正如 t3dodson 所建议的那样 - 您此时需要执行。只有你需要在前面加上一个“./”,这样它才能找到你的文件。

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

    我猜你的版本中的“节点”是“意外标识符”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      相关资源
      最近更新 更多