DEV Community

Discussion on: Various ways of handling environment variables in React and Node.js

Collapse
 
myogeshchavan97 profile image
Yogesh Chavan

There is no need of activating .env file. For .env.prod you can use the env-cmd npm package and use it as shown below:

"scripts": {
 "start": "env-cmd -f .env.dev node index.js",
 "start-prod": "env-cmd -f .env.prod node index.js"
},
Enter fullscreen mode Exit fullscreen mode

So when you run npm run start command, all .env.dev environment variables will be available through process.env.some_key and when you run npm run start-prod command, all .env.prod environment variables will be available through process.env.some_key only.

Collapse
 
rohan2734 profile image
rohan2734

ok you left one point , please answer that too
i have asked, that if i have .env.dev file
and i have some key as

#.env.dev

SOME_KEY = somekeyvalue
Enter fullscreen mode Exit fullscreen mode

then how do i need to use it?
Do i need to use it as

process.env.SOME_KEY
Enter fullscreen mode Exit fullscreen mode

or

Process.env.dev.SOME_KEY
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
myogeshchavan97 profile image
Yogesh Chavan

you need to use process.env.SOME_KEY only. Also don't add spaces before and after the = sign in .env file

Thread Thread
 
rohan2734 profile image
rohan2734

okay thankyou sir, i will definitely try this

so,

#.env.dev
SOME_KEY=sdfs
Enter fullscreen mode Exit fullscreen mode

and importing it as

process.env.SOME_KEY
Enter fullscreen mode Exit fullscreen mode

I think this is fine now

Thread Thread
 
myogeshchavan97 profile image
Yogesh Chavan

Yes, it looks fine