简体   繁体   中英

How to use command line params while running npm for 2 scripts

I have the following scripts in the package.json:

"scripts": {
   "test" : "npm run module1 || npm run posttest",
   "createenv": "node cliTest.js && npm run test"
}

cliTest.js has the following:

console.log(process.argv);

I need to run createenv first to create an env file that will be used by the script 'test'. The problem is that the arguments in the CLI are not made available.

So, if I run the following:

npm run createenv foobar

I get the following and I do not see 'foobar'

[
  '/Users/xxxxx/.nvm/versions/node/v16.13.2/bin/node',
  '/Users/xxxxx/Documents/core/cliTest.js'
]

How can I retrieve the value foobar from the CLI in my cliTest.js file?

If you really need this &&-chain you can try npm_config_ *, something like this:

"createenv": "node cliTest.js ${npm_config_name} && npm run test"

then run the command:

npm run createenv --name=foobar

the last argument in console.log should be 'foobar'

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