JavaScript

Node.js 20.6 adds built-in support for .env files

Advertisements

Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform and gives us the ability to load environment variables from .env files directly without using third-party packages. While it is great to see first-class support, some caveats remain.

Let us look at how it works. Assuming that you are running Node 20.6, create a .env file:

API_KEY="KEY"
DATABASE_URL="URL"
JavaScript

And then, you can run node using the following command:

node --env-file .env index.js
JavaScript

This would give you access to the variables defined in the .env file in your JavaScript code.

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

// URL
JavaScript

That is it! Want to have a different production configuration? Just create a new file and point it to a .env.production file.

Order when running the command matters

A minor detail to remember when executing the script is that the env file needs to be passed in before the file name. Ideally, it should have been interchangeable, but that is not the case. The env file gets ignored if we use the command:

// .env file gets ignored in this case
node inex.js --env-file .env
JavaScript

Caveats

As with all experimental things, a few things are missing. Some of these might lead to people using dotenv until support for these gets added. I will mention them here and let you see if they are dealbreakers. You can also follow the GitHub issue to track missing feature support.

No Multiline Support

Multiline environment variables are not supported currently. If you add one, it will be undefined.

// .env
WORLD="Hello
World"


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

// running the script
node --env-file=.env index.js
Hello undefined
JavaScript

The same variable is defined in both the environment and file

If the same variable is defined in the environment and the file, the value from the file takes precedence. There is no way to override it with the system environment variables.

// .env
WORLD="foo"


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

// running the script
export WORLD="bar"
node --env-file=.env index.js
Hello foo
JavaScript

Note: This has been changed in version 20.7.0, where values defined in the environment variable take precedence.

No variable references/expansions

Node does not support variable expansion currently. It will output the variable as a string if trying to reference another variable using $variable. This is possible in dotenv using the dotenv-expand library.

// .env
WORLD="foo"
WORLD_BAZ=$WORLD


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

// running the script
node --env-file=.env index.js
Hello $WORLD
JavaScript

No .env.vault support

dotenv-vault is another popular package that lets you encrypt your secret and decrypt the file just in time. They are quite helpful for production and CIT environments but are not supported currently.

Conclusion

Node.js 20.6 adding built-in support for .env files is a huge step forward for the Node community. Hopefully, it does not stay experimental in the near future, and we can start using it in our applications soon!

Saransh Kataria

Born in Delhi, India, Saransh Kataria is the brain behind Wisdom Geek. Currently, Saransh is a software developer at a reputed firm in Austin, and he likes playing with new technologies to explore different possibilities. He holds an engineering degree in Computer Science. He also shares his passion for sharing knowledge as the community lead at Facebook Developer Circle Delhi, NCR which is a developer community in Delhi, India.

Share
Published by
Saransh Kataria
Tags: nodenode.js

Recent Posts

Remapping keyboard keys to avoid Carpal Tunnel

I am terrible at optimizing my keyboard layout for anything. But off lately, my little…

2 months ago

Fixing cookies are blocked for a website with shields down on Brave

I recently switched completely to the Brave browser and have set ad blocking to aggressive…

6 months ago

Generating a QR code using Node.js

I was preparing a slide deck for a hackathon and decided to put in a…

7 months ago

How to clear the global npx cache

I have been using npx a lot lately, especially whenever I want to use a…

8 months ago

Copy/Pasting output from the terminal

Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…

8 months ago

How To Get The Hash of A File In Node.js

While working on a project, I wanted to do an integrity check of a file…

9 months ago
Advertisements