节点(SyntaxError:意外标识符)js终端中的文件

Tho*_*dez 4 javascript terminal node.js

当我尝试通过带有节点的终端运行javascript文件时,我得到"SyntaxError:unexpected identifier"


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

console.log('hello world');
Run Code Online (Sandbox Code Playgroud)


这是我的终端发生的事情.

> 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)
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*hip 5

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

> Thoms-MacBook-Pro:desktop thomvaladez$ node example.js
Run Code Online (Sandbox Code Playgroud)

如果你已经在你已经完成的节点会话中 - 正如t3dodson建议的那样 - 你在那时做了一个要求.只需要在"./"前加上它,以便找到你的文件.

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

我猜你的版本中的"节点"是"意外的标识符".