-1

I have following package.json file:

.....
"scripts": {
  "script1": "node script1.js",
  "script2": "npm run script1 && node script2.js"
},
.....

> npm run script2 --port '8081'

Now, flag --port is used only in script2. How can I pass this flag in script1?

alexdf
  • 211
  • 2
  • 7
  • A cross-platform solution is to run; `npm run script2 -- --port 8081` - Note the additional `--` between the `npm run script2` command and your arguments `--port 8081` – RobC Mar 27 '20 at 07:49

1 Answers1

2

I've found its easier to use environment variables in cases like this:

PORT=8081 npm run script2

Then have your scripts use that variable like so:

"scripts": {
  "script1": "node script2.js --port=${PORT:-8080}",
  "script2": "npm run script1 && node script2.js --port=${PORT:-8080}"
}
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • This is ok when running on _*nix_ platforms because the default shell that npm utilizes for npm scripts is `sh`. However Command Substitution, i.e. `${...}`, on Windows will fail because typically the default shell that npm utilizes for npm scripts is `cmd`, – RobC Mar 27 '20 at 07:45
  • This is true. I've just accepted that and use Ubuntu on Windows always. I've given up on cmd and hope someday windows will as well. – justin.m.chase Mar 27 '20 at 22:00