如何通过npm运行脚本将标志传递给nodejs应用程序? [英] How to pass flags to nodejs application through npm run-script?

查看:115
本文介绍了如何通过npm运行脚本将标志传递给nodejs应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've a NodeJS file that I run via an "npm" command. I've been trying to list all arguments (including flags). If I run it by directly calling the node exe, it works fine but if I use the npm command, I cannot access the flags.

代码:

console.dir(process.argv);

当我这样运行文件时,

node file.js arg1 arg2 -f --flag2

我可以得到所有参数.

[ '/usr/local/bin/node',
  '/.../file.js',
  'arg1',
  'arg2',
  '-f',
  '--flag2' ]

但是,如果我向package.json文件中添加一个npm运行程序并尝试使用它运行,那么我只能获取参数,而不能获取标志.

But if I add an npm runner to the package.json file and try to run with it, I can only get the arguments but not the flags.

npm run test arg1 arg2 -f --flag2

结果:

[ '/usr/local/bin/node',
  '/.../file.js',
  'arg1',
  'arg2' ]

package.json文件:

package.json file:

{
  "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"
}

基本上,节点不会将标志传递给正在运行的脚本.有解决方案吗?我的真实项目的路途很长,包含很多参数,因此我想使用快捷方式对其进行测试.

Basically, node won't pass flags to the running script. Is there a solution for this? My real project has a long path with a lot of arguments so I want to use a shortcut to test it.

推荐答案

在参数前使用双破折号:

Use a double dash before arguments:

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

这篇关于如何通过npm运行脚本将标志传递给nodejs应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆