1

Let's say I have the following in a file called print-last-arg.js:

console.log(process.argv[process.argv.length-1])

And the following scripts in my package.json:

"scripts": {
  "print_a": "node print-last-arg.js",
  "print_b": "npm run print_a"
}

When I run npm run print_a -- --foo=bar, I get --foo=bar as expected.

However, npm run print_b -- --foo=bar gives me no output.

How do I pass the CLI arguments from print_b to print_a?

ethan.roday
  • 2,485
  • 1
  • 23
  • 27
  • 1
    Possible duplicate of [How to pass a command line argument to a nested script?](https://stackoverflow.com/questions/40495116/how-to-pass-a-command-line-argument-to-a-nested-script) – RobC Nov 22 '18 at 09:03
  • Agreed, @RobC - didn’t find that one. Thanks. – ethan.roday Nov 22 '18 at 21:09

1 Answers1

2

It turns out that you just have to add an extra -- on the end of print_b, which will tell npm to pass whatever arguments print_b got to print_a. So,

"scripts": {
  "print_a": "node print-last-arg.js",
  "print_b": "npm run print_a"
}

becomes

"scripts": {
  "print_a": "node print-last-arg.js",
  "print_b": "npm run print_a -- "
}

Voilà! Now npm run print_b -- --foo=bar prints --foo=bar as expected.

ethan.roday
  • 2,485
  • 1
  • 23
  • 27