如何使用harmony标志启动全局npm模块

14

我写了一个可以全局安装的 npm 模块 dm-npm

我喜欢在那个模块中使用 co。

当全局启动时,我该如何告诉模块以 harmony 标志运行?

这是 package.json 文件:

{
  "name": "dm-npm",
  "version": "0.0.3",
  "description": "npm helper",
  "main": "index.js",
  "scripts": {
    "test": "mocha --reporter nyan",
    "start": "node --harmony ./bin/dm-npm"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/divramod/dm-npm.git"
  },
  "keywords": [
    "npm",
    "template"
  ],
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/divramod/dm-npm/issues"
  },
  "homepage": "https://github.com/divramod/dm-npm",
  "devDependencies": {
    "chai": "^2.1.0",
    "mocha": "^2.1.0"
  },
  "dependencies": {
    "co": "^4.4.0",
    "co-prompt": "^1.0.0",
    "colors": "~1.0.3",
    "shelljs": "^0.3.0"
  },
  "bin": {
    "dmnpm": "./bin/dm-npm"
  }
}

当我使用协程运行时,遇到以下错误信息:

> $ dmnpm init                                                                                                                         
/usr/local/lib/node_modules/dm-npm/index.js:152
co(function*() {
           ^
SyntaxError: Unexpected token *
    at exports.runInThisContext (vm.js:73:16)
    at Module._compile (module.js:443:25)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/dm-npm/bin/dm-npm:3:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

它是由...引起的。

co(function*() {
    var projectName =
        yield prompt('project name: '.blue);
    process.stdin.pause();
});

一个模块不能决定 node.js 启动时使用哪些命令行标志。如果这就是你的问题,那么模块的用户将不得不使用 --harmony 标志启动 node.js,并且你的模块文档也必须告知他们如何操作。 - jfriend00
我正在命令行中启动模块。我在我的zshrc中创建了一个别名“alias node ='node --harmony'”,但我仍然遇到同样的问题。我应该在哪个位置定义node始终使用harmony标志运行? - divramod
1个回答

10

#!/usr/bin/env node --harmony

这行代码放在脚本的顶部,对我来说很有效,在你的情况下是在 /bin/dm-npm 中。


1
我必须使用 #!/usr/bin/node --harmony - ryanpcmcquen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接