0

I have the following node script to start a react app. It sets a variamle in the .env. Is there a way to set this script in the package.json, so it can take a parameter from the terminal?

script in the terminal that sets a color in the .env:

set COLOR=blue&& node scripts/start.js

package.json, how to hand over the color parameter?

"scripts": {
  "startWithColor": "set COLOR=$COLOR&& node scripts/start.js",
RobC
  • 22,977
  • 20
  • 73
  • 80
vuvu
  • 4,886
  • 12
  • 50
  • 73
  • Possible duplicate of [Cross-platform way to pass environment variables as arguments to npm scripts](https://stackoverflow.com/questions/52656882/cross-platform-way-to-pass-environment-variables-as-arguments-to-npm-scripts) – RobC Oct 18 '19 at 15:09
  • 1
    Given my answer at the link in the comment above. You need to make the following changes to work for your scenario: **1)** Change the last line of the node.js utility script to `execSync(\`cross-env ${args.join(' ')} node scripts/start.js\`, {stdio:[0, 1, 2]});` **2)** Install [cross-env](https://www.npmjs.com/package/cross-env), i.e. run `npm i -D cross-env` in your project directory. **3)** Change your npm-script in _package.json_ to `"startWithColor": "node start",` (note: this points to the node.js utility script). **4)** To invoke your script run `npm run startWithColor -- --COLOR=blue` – RobC Oct 18 '19 at 15:47

0 Answers0