3

I have a javascript file, do-stuff.js, which does stuff with the arguments received from command line

// do-stuff.js
doSomething(process.argv[2]);
doSomethingElse(process.argv[3]);

I can execute do-stuff with an npm script like this

"scripts": {
  "do-stuff": "node do-stuff"
}

I call the script from command line and everything works fine

npm run do-stuff -- --firstArg=abc --secondArg=123

Now I create a second file, do-more-stuff.js which I want to execute sequentially, after having executed do-stuff.js.

So I create a second script

"scripts": {
   "do-stuff": "node do-stuff",
   "do-more-stuff": "npm run do-stuff && npm run do-more-stuff",
}

But now, if I call do-more-stuff with the same arguments as before

npm run do-more-stuff -- --firstArg=abc --secondArg=123

I see that the parameters are not passed to do-stuff.

Is there a way to make an npm script which executes commands sequentially to be able to receive arguments from command line?

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Picci
  • 16,775
  • 13
  • 70
  • 113
  • Possible duplicate of [Pass command line args to npm scripts in package.json](https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json) – RobC Mar 27 '19 at 11:08
  • 1
    The issue is that npm does not, nor intends to, provide a builtin feature which allows arguments to be passed to the middle of a npm script - as stated [here](https://github.com/npm/npm/pull/5518#issuecomment-46915480). However my [answer](https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json#answer-51401577) to the possible duplicate question provides a solution(s) to overcome this limitation. – RobC Mar 27 '19 at 11:18
  • 1
    Using `node_config_*` variables might work in this case https://stackoverflow.com/a/60458728/728287 – Gianfranco P. Mar 11 '20 at 22:58

0 Answers0