Good day. The question is simple, how to pass flags/options to npm-run-script and read them using commander.js.
Example:
package.json
{
"scripts": {
"start": "node index.js"
},
"dependencies": {
"commander": "^12.1.0"
}
}
index.js
const { program } = require("commander");
program.option("-p --config-path <path>", "config path", ".").parse();
console.log(`options: ${ JSON.stringify(program.opts()) }`);
Running 1 and 2, works as expected:
node index.js -p "/path/foo/boo"
node index.js --config-path="/path/foo/boo"
results in: options: {"configPath":"/path/foo/boo"}
Running npm start
(used special --
)
npm start -- -p="/path/foo/boo"
npm start -- --config-path="/path/foo/boo"
results in:
> start
> node index.js
options: {"configPath":"."}
configPath
option received default value "."
.
Can the same result be achieved with npm-run-script
?
This is a problem with recent versions of npm on PowerShell. You could try an older version of npm, or using a different shell, or using --
twice as a temporary work-around!
There is more detail in this npm issue: https://github.com/npm/cli/issues/7375