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

In a package.json script, how do I pass an argument to node, as opposed to the script?

我的 package.json 中有一个如下所示的脚本:

  "scripts": {
    "test": "... && mocha --full-trace test/db test/http test/storage test/utils",

我想将 --async-stack-traces 传递给 mocha,但是 --async-stack-traces 是节点命令行参数,而不是 mocha 命令行参数。

我想我可以通过 运行 node --async-stack-traces node_modules/mocha/bin/mocha --full-trace test/db test/http test/storage test/utils 来实现这一点,但感觉有些不雅或不合时宜。还有其他方法可以实现吗?

你写的方式基本上就是这样做的方式:

node --async-stack-traces node_modules/mocha/bin/mocha --full-trace test/db test/http test/storage test/utils

您可以像这样使用 node_modules/.bin 稍微整理一下:

node --async-stack-traces node_modules/.bin/mocha --full-trace test/db test/http test/storage test/utils

下一个可能不 Windows 兼容并且不容易理解,但如果您只支持类 UNIX 操作系统,您可以依赖 $PATH 注入 npm 脚本来整理一下:

node --async-stack-traces `which mocha` --full-trace test/db test/http test/storage test/utils

如果不想依赖whereis,可以使用npm bin:

node --async-stack-traces `npm bin`/mocha --full-trace test/db test/http test/storage test/utils

希望在这一点上,您正在举手并回到上面的第一个或第二个选项。 (我推荐第二个选项,所以你不依赖于 mocha 包布局,可以想象它可能会改变。)