问题 如何用和声标志启动全局npm模块


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

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

我怎么能告诉模块它在全局启动时以和声标志运行?

这是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"
  }
}

使用co函数运行时出现以下错误消息:

> $ 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();
});

12419
2018-02-27 02:57


起源

模块无法指示node.js启动的命令行标志。如果这就是你所要求的,那么模块的用户将只需要启动node.js --harmony flag和你的模块的文档只需通知他们。 - jfriend00
我从命令行启动模块。我在我的zshrc“alias node ='node --harmony'”中创建了一个别名,但我遇到了同样的问题。我可以在哪个地方定义该节点始终以和声标志运行? - divramod


答案:


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

脚本顶部适合我,在你的情况下在/ bin / dm-npm


9
2018-03-01 21:46



thx,这解决了我的问题。我写道:#!node --harmony - divramod
很高兴知道这是可能的,谢谢。 - James Wright
我不得不使用 #!/usr/bin/node --harmony。 - ryanpcmcquen