0

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:

  • (1) node index.js -p "/path/foo/boo"
  • (2) node index.js --config-path="/path/foo/boo"

results in: options: {"configPath":"/path/foo/boo"}

result-ok


Running npm start (used special --)

  • (1) npm start -- -p="/path/foo/boo"
  • (2) 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?

similar question

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. Work-around works (using -- twice). npm start -- -- --config-path="/path/foo/boo"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.