Jon
Jon

Reputation: 8511

How to pass argument into NPM script without double dashes

//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 --?

Upvotes: 3

Views: 1466

Answers (1)

tveal
tveal

Reputation: 96

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.

Upvotes: 3

Related Questions