Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过npm运行脚本将标志传递给nodejs应用程序?_Javascript_Json_Node.js_Package.json - Fatal编程技术网

Javascript 如何通过npm运行脚本将标志传递给nodejs应用程序?

Javascript 如何通过npm运行脚本将标志传递给nodejs应用程序?,javascript,json,node.js,package.json,Javascript,Json,Node.js,Package.json,我有一个通过“npm”命令运行的NodeJS文件。我一直试图列出所有参数(包括标志)。如果我通过直接调用node exe来运行它,它可以正常工作,但是如果我使用npm命令,我就无法访问这些标志 npm run test arg1 arg2 -f --flag2 代码: 当我像这样运行文件时 node file.js arg1 arg2 -f --flag2 我可以得到所有的论点 [ '/usr/local/bin/node', '/.../file.js', 'arg1', 'a

我有一个通过“npm”命令运行的NodeJS文件。我一直试图列出所有参数(包括标志)。如果我通过直接调用node exe来运行它,它可以正常工作,但是如果我使用
npm
命令,我就无法访问这些标志

npm run test arg1 arg2 -f --flag2
代码:

当我像这样运行文件时

node file.js arg1 arg2 -f --flag2
我可以得到所有的论点

[ '/usr/local/bin/node',
  '/.../file.js',
  'arg1',
  'arg2',
  '-f',
  '--flag2' ]
但是,如果我向package.json文件添加一个npm运行程序并尝试使用它运行,我只能获取参数,而不能获取标志

npm run test arg1 arg2 -f --flag2
结果是:

[ '/usr/local/bin/node',
  '/.../file.js',
  'arg1',
  'arg2' ]
package.json文件:

{
  "name": "name",
  "version": "1.0.0",
  "description": "",
  "main": "test.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "test" : "node test.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

基本上,节点不会将标志传递给正在运行的脚本。有解决办法吗?我真正的项目路径很长,有很多参数,所以我想用一个快捷方式来测试它。

在参数前用双破折号:

npm run test -- arg1 arg2 -f --flag2

如果您的参数看起来像
--reset
,您可以使用
npm run test--reset
我不确定这有多大区别,但我试图执行“npm run ng serve-o”,但为了识别“o”(用于打开新的浏览器窗口),我必须像“npm run ng serve--o”一样键入它在“o”前面有一个单破折号,而不是双破折号。