【问题标题】:Node.js can't compile my .js files (OSX Yosemite)Node.js 无法编译我的 .js 文件(OSX Yosemite)
【发布时间】:2015-07-12 02:25:52
【问题描述】:

我在我的 macbook pro 上运行 node.js,我真的不明白为什么每次我尝试通过我的终端编译某些东西时都会收到这个错误:

MBPdiFrancesco:~ francescocolonnese$ node -p
> /Users/francescocolonnese/Desktop/appunti.js 
SyntaxError: Invalid flags supplied to RegExp constructor 'francescocolonnese'
    at new RegExp (native)
    at repl:1:1
    at REPLServer.defaultEval (repl.js:132:27)
    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)

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你能显示有问题的代码吗?
  • 确定@informaniac !我将在下面发短信..但它发生在我运行节点后放在终端上的所有 .js 文件中。这是一个例子: var ciao =12; var boh = “nooo”; console.log(ciao+boh); 你可以看,代码真的很简单,但它不起作用。更复杂的代码也会发生这种情况。
  • 您使用的是什么版本的 Node?我看到你的字符串周围有一些奇怪的引号 - 确保这些是常规引号;当我修复它们时它对我有用 - 但我无法重现 SyntaxError: Invalid flags supplied to RegExp constructor 'francescocolonnese' 错误。您是否提供了您的节点失败的确切代码?
  • 是的,我刚刚尝试了几个不同的文件,但每个文件都出现相同的错误。 我正在使用节点 0.12.2
  • 顺便说一句,我更正了引号,我得到了这个: SyntaxError: Unexpected token ILLEGAL at Object.exports.createScript (vm.js:44:10) at REPLServer.defaultEval (repl.js :117:23) 绑定 (domain.js:254:14) 在 REPLServer.runBound [as eval] (domain.js:267:12) 在 REPLServer. (repl.js:279:12) 在 REPLServer .emit (events.js:107:17) 在 REPLServer.Interface._onLine (readline.js:214:10) 在 REPLServer.Interface._line (readline.js:553:8) 在 REPLServer.Interface._ttyWrite (readline. js:830:14) 在 ReadStream.onkeypress (readline.js:109:10)

标签: javascript node.js macos terminal


【解决方案1】:

等一下,您是否尝试通过在节点交互式 shell 中输入 js 文件的名称来运行它?如果是这样,错误的原因很简单。这是因为:

/Users/francescocolonnese/Desktop/appunti.js

不是有效的 JavaScript 代码。实际上,查看错误您可以看到节点尝试将// 解释为正则表达式:

/Users/francescocolonnese

并抱怨francescocolonnese 不是有效的正则表达式开关。

在节点外壳中,您应该只键入有效的 javascript。如果您坚持这样做,您可以输入:

require('/Users/francescocolonnese/Desktop/appunti.js')

但这是错误的做法。相反,只需让 node.js 运行该文件:

node -p /Users/francescocolonnese/Desktop/appunti.js

【讨论】:

  • MBPdiFrancesco:~ francescocolonnese$ node > node -p /Users/francescocolonnese/mail.js ReferenceError: node is not defined at repl:1:1 at REPLServer.defaultEval (repl.js:132:27 ) 在 REPLServer.runBound 的绑定 (domain.js:254:14) [as eval] (domain.js:267:12) 在 REPLServer. (repl.js:279:12) 在 REPLServer.emit (events .js:107:17) 在 REPLServer.Interface._onLine (readline.js:214:10) 在 REPLServer.Interface._line (readline.js:553:8) 在 REPLServer.Interface._ttyWrite (readline.js:830: 14) 在 ReadStream.onkeypress (readline.js:109:10
  • 从 bash 提示符 ($) 运行 node -p /Users/francescocolonnese/Desktop/appunti.js,而不是从节点提示符 (&gt;)
  • @FrancescoColonnese:如果您仍然坚持在节点交互式外壳中运行脚本,请使用我上面提到的require
【解决方案2】:

"@infomaniac - 我会尝试删除整个文件夹:/usr/local/share/systemtap/"

为我工作,谢谢!

卸载 node.js

brew uninstall node

brew uninstall node --force

brew doctor 的输出:

正在链接 /usr/local/Cellar/node/0.12.4... 错误:无法符号链接 share/systemtap/tapset/node.stp /usr/local/share/systemtap/tapset 不可写。

删除 /usr/local/share/systemtap/

$ sudo rm -rf /usr/local/share/systemtap/

重新安装 node.js

brew install node

修复未链接的小桶

$ brew link node

输出:

正在链接 /usr/local/Cellar/node/0.12.4... 已创建 6 个符号链接

【讨论】:

    【解决方案3】:

    卸载你从nodejs.org安装的node版本,然后运行:

    brew update

    brew uninstall node --force

    brew install node

    【讨论】:

    • Bash 补全已安装到:/usr/local/etc/bash_completion.d 错误:brew link 步骤未成功完成 公式已构建,但未符号链接到 /usr/local 可以不是符号链接 share/systemtap/tapset/node.stp /usr/local/share/systemtap/tapset 不可写。您可以使用重试:brew link node 警告:安装后步骤未成功完成您可以使用brew postinstall node 重试
    • 尝试以sudo运行以上所有方法
    • 我在运行“sudo brew install node”时得到这个错误:懦弱地拒绝sudo brew install 您可以将 brew 与 sudo 一起使用,但前提是 brew 可执行文件由 root 拥有。但是,这既不推荐也完全不受支持,因此后果自负。
    • 我会尝试删除整个文件夹:/usr/local/share/systemtap/
    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 2016-01-14
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    相关资源
    最近更新 更多