kentcdodds / cross-env

🔀 Cross platform setting of environment scripts
https://www.npmjs.com/package/cross-env
MIT License
6.32k stars 243 forks source link

cross-env not setting custom process environment variables on windows OS #208

Closed maheshkrsna closed 5 years ago

maheshkrsna commented 5 years ago

Relevant code or config

package.json > "scripts"

"start": "cross-env DEBUG=true && webpack-dev-server";

webpack.config.js

mode: process.env.DEBUG ? 'development' : 'production',

What you did: add the above code snippets in a react project and run it as an npm script on windows OS.

What happened: React code runs with production version instead of debug version. Looks like cross-env isn't setting the debug variable. There are no errors thrown though.

Reproduction repository:

Problem description: cross-env isn't setting custom process environment variables on windows OS.

Suggested solution:

crebier-corentin commented 5 years ago

Remove the && and retry

"start": "cross-env DEBUG=true webpack-dev-server";
crebier-corentin commented 5 years ago

Just tested on windows

//index.js
console.log(process.env.NODE_ENV);
"scripts": {
  "dev": "cross-env NODE_ENV=dev node index.js",
  "prod": "cross-env NODE_ENV=prod node index.js",
  "error": "cross-env NODE_ENV=dev && node index.js"
}

dev prints "dev" prod prints "prod" error prints undefined

maheshkrsna commented 5 years ago

@bibo5088 Yes, removing the && worked. Thanks.

rbhgaston commented 4 years ago

May I ask why I have to remove the && ? @bibo5088

crebier-corentin commented 4 years ago

&& is used in shells to run multiple commands in sequence.
So cross-env DEBUG=true && webpack-dev-server would be read as run command cross-env DEBUG=true and after that run command webpack-dev-server.
cross-env would not receive webpack-dev-server as an argument and wouldn't run it.

cross-env doesn't modify the current instance's variables, rather it spawns a new shell and runs the command in it.