简体   繁体   English

在 package.json 脚本中,我如何将参数传递给 node,而不是脚本?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM