DEV Community

Cover image for Node.js 20.6 adds built-in support for .env files
saransh kataria
saransh kataria

Posted on • Updated on • Originally published at wisdomgeek.com

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

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"
Enter fullscreen mode Exit fullscreen mode

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

node --env-file .env index.js
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode




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

Enter fullscreen mode Exit fullscreen mode




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

Enter fullscreen mode Exit fullscreen mode




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

Enter fullscreen mode Exit fullscreen mode




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!

Top comments (8)

Collapse
 
moopet profile image
Ben Sinclair

This is something I always find weird - platform xxx implements a feature you'd think would be in at version 1, but it takes them a decade to do so, during which time a raft of other solutions have flooded the ecosystem. Happens a lot, dunno why!

Collapse
 
saranshk profile image
saransh kataria

Standards take some time because they evolve with time and as community support for a feature increases, features get added to the default platform.

Collapse
 
harry176 profile image
Harry

Nice post @saranshk . How would I utilise this feature if I'm using a framework like Gatsby? I run the command gatsby develop instead of node.

Collapse
 
yzqdev profile image
yzqdev

like this? you could have a try.

node --env-file=.env ./node_modules/gatsby/dist/bin/gatsby.js develop
Enter fullscreen mode Exit fullscreen mode
Collapse
 
saranshk profile image
saransh kataria

I believe this should work. Though from reading this, it looks like Gatsby already has dotenv imported, and you might want to continue using it if that is the case, or you can choose not to use it.

Thread Thread
 
harry176 profile image
Harry

Thank you both.

Collapse
 
tempestrock profile image
Tempest Rock • Edited

In the section “The same variable is defined in both the environment and file“,shouldn‘t the output be ‘Hello bar‘ according to your explanations?

Collapse
 
saranshk profile image
saransh kataria

Sorry, I wrote an incorrect explanation in there. I have updated it now. Thank you for pointing it out!