Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument into NPM script without double dashes

Tags:

npm

//package.json    
"scripts": {
    "run-tests": "node scripts/run-tests.js"
}

When I run my test suite using npm run run-tests --env=integration --variant=alpha, I get undefined argument values:

//run-tests.js
console.log(argv.env) //undefined
console.log(argv.variant) //undefined

However, when I run my test suite with two dashes (--) npm run run-tests -- --env=integration --variant=alpha, I get my argument values:

//run-tests.js
console.log(argv.env) //integration
console.log(argv.variant) //alpha

Can I somehow get my argument values in run-test.js without using --?

like image 953
Jon Avatar asked Dec 04 '25 00:12

Jon


1 Answers

Based on the discussion in this pull request, I believe the answer to your question is no :(

However, a workaround is to specify your arguments in the package.json file in the scripts block. This is preferred in a CI/CD context, as you want less coupling with your CI/CD provider.

In package.json

"scripts": {
    "test:int:a": "node scripts/run-tests.js --env=integration --variant=alpha",
    "test:int:b": "node scripts/run-tests.js --env=integration --variant=bravo"
}

Then on command-line:

npm run test:int:a

If you are going more for a command-line tool, I suggest looking into creating a CLI tool with node.js, such as this example.

like image 192
tveal Avatar answered Dec 07 '25 16:12

tveal