Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting NODE_ENV differs between Mac and Windows

I work with a team of 4 and I'm the only Mac user and my peers are using Windows machine. My team members (Windows users) use the following which was not working on my Mac machine:

"start": "set \"NODE_ENV=prod\" && node index.js"

And when I tried to print process.env.NODE_ENV in log it returned undefined.

Then after browsing, I used the following on my Mac and it was working fine:

"start: "export NODE_ENV=prod && node index.js"

Now if I commit my code in repository they will be affected.

Is there any common solution which can be used to get rid of this platform issue?

Thanks in advance.

like image 581
Selvam Raju Avatar asked Jun 11 '26 23:06

Selvam Raju


2 Answers

You can use the cross-env package to set environment variables in a cross-platform way.

To install it:

npm install --save-dev cross-env

To use it:

"start": "cross-env NODE_ENV=prod node index.js"
like image 167
Mike Cluck Avatar answered Jun 14 '26 11:06

Mike Cluck


An alternative solution could be keeping all your environment variables in the respective node environment the code is running in. And for local development, you could add a .env file and use a package like dotenv to populate the environment variables.

like image 24
Arpan Kc Avatar answered Jun 14 '26 11:06

Arpan Kc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!