Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

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

Closed
maheshkrsna opened this issue Sep 2, 2019 · 5 comments
Closed

Comments

@maheshkrsna
Copy link

  • cross-env version: ^5.2.1
  • node version: 10.15.1
  • npm version: 6.4.1

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
Copy link
Contributor

Remove the && and retry

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

@crebier-corentin
Copy link
Contributor

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
Copy link
Author

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

@rbhgaston
Copy link

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

@crebier-corentin
Copy link
Contributor

crebier-corentin commented Aug 9, 2020

&& 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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants