[英]In a package.json script, how do I pass an argument to node, as opposed to the script?
I've got a script in my package.json
that looks like this:我的
package.json
中有一个脚本,如下所示:
"scripts": {
"test": "... && mocha --full-trace test/db test/http test/storage test/utils",
I want to pass --async-stack-traces
into mocha, but --async-stack-traces
is a node command line argument, not a mocha command line argument.我想将
--async-stack-traces
传递给 mocha,但是--async-stack-traces
是节点命令行参数,而不是 mocha 命令行参数。
I think I could achieve this by running node --async-stack-traces node_modules/mocha/bin/mocha --full-trace test/db test/http test/storage test/utils
, but something about that feels inelegant or non-idiomatic.我想我可以通过运行
node --async-stack-traces node_modules/mocha/bin/mocha --full-trace test/db test/http test/storage test/utils
,但有些东西感觉不雅或非惯用语。 Is there another way to achieve this?有没有另一种方法来实现这一目标?
The way you wrote it is basically the way to do it:你写的方式基本上就是这样做的方式:
node --async-stack-traces node_modules/mocha/bin/mocha --full-trace test/db test/http test/storage test/utils
You can tidy it up a little bit by using node_modules/.bin
like this:您可以像这样使用
node_modules/.bin
稍微整理一下:
node --async-stack-traces node_modules/.bin/mocha --full-trace test/db test/http test/storage test/utils
This next one probably isn't Windows compatibile and is not as easily understandable, but if you only support UNIX-like operating systems, you can rely on the $PATH
injection of npm
scripts to tidy it a bit more:下一个可能不是 Windows 兼容的,也不那么容易理解,但如果你只支持类 UNIX 操作系统,你可以依靠
npm
脚本的$PATH
注入来稍微整理一下:
node --async-stack-traces `which mocha` --full-trace test/db test/http test/storage test/utils
If you don't want to rely on whereis
, you can use npm bin
:如果你不想依赖
whereis
,你可以使用npm bin
:
node --async-stack-traces `npm bin`/mocha --full-trace test/db test/http test/storage test/utils
Hopefully at this point, you're throwing up your hands and going back to the first or second option above.希望在这一点上,您举手并回到上面的第一个或第二个选项。 (I recommend the second option, so you're not dependent on the
mocha
package layout, which conceivably could change.) (我推荐第二个选项,所以你不依赖于
mocha
包布局,可以想象这可能会改变。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.