简体   繁体   中英

Passing a command line argument or flag to NPM package.json scripts

I am looking for away to do something like this:

package.json

...
"scripts":{

      "debug_mocha_test":"node-debug ./node_modules/mocha/bin/_mocha --grep ${names}"
  }
...

so then at the command line, I can then run something like:

npm run debug_mocha_test --names 'test1'

or whatever

does anyone know how to do this, or is there a better way than this?

For some context, the mocha testing library has a --grep function like so:

http://mochajs.org/

Since npm 2.0.0, it's possible to pass arguments to a script defined in the package.json file ( https://github.com/npm/npm/pull/5518 ). The format is not as clean as one would expect, but it works.

To pass the "foo" parameter to the "start" script:

npm run-script start -- foo=1

So in your case, the script in package.json should be:

"debug_mocha_test":"node-debug ./node_modules/mocha/bin/_mocha --grep"

And you can run it using:

npm run debug_mocha_test -- test1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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