DEV Community

Cover image for Using dotenv to manage environment variables in Nodejs
Hridayesh Sharma
Hridayesh Sharma

Posted on

Using dotenv to manage environment variables in Nodejs

Have you ever faced any of these scenarios:

  1. You have some dynamic values in your code which you don't want to hardcode.
  2. You have API Keys in your opensource project which you obviously don't want to push to GitHub
  3. You have some values in your code that depend on what environment you are building your code for.

If you fall into any of the above-mentioned scenarios I have a got a solution for you. 😃

Node Environment Variables Using dotenv

In Node, there is something called environment variables which you can see by logging process.env.
For example, you can set a NODE_ENV variable which is basically used to define what environment you want to build your code for.

process.env.NODE_ENV='dev'
Enter fullscreen mode Exit fullscreen mode

Then further use this variable to generate different builds.

Instead of setting these environment variables ourselves, we will be using the dotenv package.

$ npm i --save dotenv 
Enter fullscreen mode Exit fullscreen mode

Once dotenv is installed let's create a .env file in the root of our project
and add the PORT variable into it.

.env

PORT=3000
Enter fullscreen mode Exit fullscreen mode

Now let's see how we can use this .env file in our code. We will create a basic node server and use PORT defined in our .env.
NOTE: If you do not understand the server part, don't worry. It's not important to understand how to use dotenv.

app.js


require('dotenv').config()
const http = require('http')

const server = http.createServer(callback)

server.listen(process.env.PORT || 4000)

Enter fullscreen mode Exit fullscreen mode

Let's see what we just did here.

We basically created a server in node and the server is listening on the port defined in our node environment variables. If it's not defined there we give it a fallback value.
Now the value of this PORT can be different depending upon where you deploy your server be it Heroku or something else.

dotenv is loaded first and it has a config function that basically reads our .env file and populates the environment variables.

You can use dotenv to store your server configuration or use it with webpack to define global variables using webpack's definePlugin.

PS:

  1. You need to load your env config once in your webpack config or in your entry file and also never commit your .env files
  2. In order to maintain multiple env files each for different environment you can use dotenv-flow package. You must set your process.env.NODE_ENV variable first as dotenv-flow uses that variable to decide which env file to pick. https://www.npmjs.com/package/dotenv-flow

If you face any problem in implementing this, let me know in the comments. I will be happy to resolve.

Thanks for reading 😀

Top comments (11)

Collapse
 
joeattardi profile image
Joe Attardi

I use dotenv in all of my Node projects now!

Collapse
 
vyasriday profile image
Hridayesh Sharma

Yeah it's great!

Collapse
 
mgrachev profile image
Grachev Mikhail

Another useful tool — github.com/dotenv-linter/dotenv-li....
It’s a lightning-fast linter for .env files. Written in Rust.

Collapse
 
denid88 profile image
Denis

After read this article, i starting use dotenv, thanks

Collapse
 
vyasriday profile image
Hridayesh Sharma

Thanks denid. This is a great motivation for me. 😃

Collapse
 
christophecraig profile image
Christophe Craig

I just learned about dotenv a few days ago and started using it but now, with dotenv-flow that will really be great ! Thanks a lot for that

Collapse
 
vyasriday profile image
Hridayesh Sharma

Glad that this post helped you. :)

Collapse
 
vjnvisakh profile image
Visakh Vijayan

How to change env variables according to environment using dotenv

Collapse
 
vyasriday profile image
Hridayesh Sharma • Edited

You can use dotenv-flow for that. I have updated the post for the same.

Collapse
 
kiransiluveru profile image
kirankumar

How to Access the ENV var in different files in Node.js using dotenv?

Collapse
 
vyasriday profile image
Hridayesh Sharma

Hi Kiran You can access ENV variables like process.env.VARIABLE_NAME once you load dotenv in your file.