How to use dotenv

cover image

We are going to kick off our first blog post with the basics – how to use dotenv.

Create your project

Begin by creating a project.

Let’s call it hello-world.

$ mkdir hello-world
$ cd hello-world
$ touch index.js

Edit the index.js file and place the following in it.

// index.js
console.log('Hello World')

Test that it runs correctly.

$ node index.js

It will output “Hello World”.

Create your .env file

A .env file is where you put all your secrets - your app configuration, api keys, and encryption keys.

Create a very simple one.

$ touch .env

Edit it.

# .env

Save those changes.

Install dotenv and require it

Run npm install dotenv to install the dotenv library.

$ npm install dotenv --save

Then edit your index.js file and require dotenv at the top of the file.

// index.js
console.log(`Hello ${process.env.HELLO}`)

When you run this it is going to first run require dotenv, run the config command which will pull in your values from your .env file, and then load those into your process.env environment variables.

Try it out. Run node index.js.

$ node index.js
Hello Universe

Great! You just used dotenv at its foundational layer!

Mar 13, 2023