我定义的NPM CLI命令作为mycommand
模块:
{
...
"name": "mycommand",
"bin": {
"mycommand": "bin/mycommand.js"
},
...
}
其中mycommand.js
看起来是这样的:
#!/usr/bin/env node --harmony
console.log('...')
我希望能够通过以下方式,安装在另一个模块运行它:
{
..
"scripts": {
"mycommand": "mycommand --require ./bootstrap"
}
...
}
其中bootstrap
是,我期望能够require
前mycommand
-ED的模块。该--require
参数应该被传递到节点运行mycommand
。
结果与尝试
bootstrap
。没有任何错误或不正确的路径解析或类似的其他指示。mycommand
脚本:
{
...
"scripts": {
"mycommand": "node --require ./bootstrap ./some-random-js-file"
}
...
}
如预期bootstrap
是必需的,证明bootstrap
存在和--require
工作。
require
的bootstrap
模块mycommand
(我真的不能做的 - 所以只是为了测试的缘故):
#!/usr/bin/env node --harmony
require('../../../bootstrap');
console.log('...')
如预期bootstrap
是必需的。需要注意的是../../../
有以补偿mycommand.js
的node_modules
内的位置。
假设
我认为--require
不会真正传递给节点执行mycommand
。
题
如何才能做到这一点?
你的假设--require
不会传递到node
是正确的:
node --require … path/to/mycommand.js
你传递参数给node
。mycommand --require …
你传递参数给mycommand
。(你可以根据需要在解析和mycommand.js
require()
命令行选项,如果适合你的使用情况)
我假设你不想因为你需要指定路径的脚本,从而失去在第一时间创建node … mycommand.js
场整点使用bin
方法。
然而,由于您提供一个bin
场,你就已经有mycommand
已知路径:
{
..
"scripts": {
"mycommand": "node --require ./bootstrap ./node_bin/mycommand"
}
...
}
或可改为… $(npm bin)/mycommand
。