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

Time:07-21

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?

CodePudding user response:

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'

  • Related