DEV Community

Discussion on: Environment variables in Node.js. The Right way!

Collapse
 
thesuhu profile image
The Suhu

why your script start prodouction did not load dotenv?

Collapse
 
numtostr profile image
Vikas Raj

dotenv is a dev dependency. In production, keys are stored on the server which can be accessed by node without using dotenv

Collapse
 
thesuhu profile image
The Suhu

can you give me little example how storing keys on production? thank you

Thread Thread
 
numtostr profile image
Vikas Raj

For example, heroku has a option in app settings to enter environment variable.

Thread Thread
 
thesuhu profile image
The Suhu

yes, in heroku there is an option to store that. but how if we use own server?

Thread Thread
 
numtostr profile image
Vikas Raj • Edited

Like this

// keys.js ======
module.exports = {
    PORT: process.env.PORT,
    WHO_AM_I: process.env.WHO_AM_I,
};
Thread Thread
 
thesuhu profile image
The Suhu

ok, thank you.

Thread Thread
 
yogendra3236 profile image
Yogendra

But, still we're using 'process.env', which uses to 'dotenv' package in production?

Thread Thread
 
thesuhu profile image
The Suhu

In production I didn't use .env, I store all credentials on host environment or if I use docker, I store it in docker secrets.

Thread Thread
 
yogendra3236 profile image
Yogendra

Cool, thanks!

Collapse
 
safinghoghabori profile image
Safin Ghoghabori

Yes thats right that we store keys on server.
But what about the dotenv package we imported into file and written as process.end.VAR_NAME ? Wont it require to use dotenv package?