npm-scriptsnpm-startnode-commander

npm-run-script with commander.js


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:

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

result-ok


Running npm start (used special --)

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


Solution

  • 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