Introduction

A long-awaited feature amongst NodeJS developers. Finally, starting from Node.js v20.6.0, Node.js supports .env files for configuring environment variables.

We used to plug in third-party dependencies for that same need, the most notable one is dotenv.

Do you prefer the Video format instead? Feel free to watch it by following this link:

The YouTube Channels in both English (En) and French (Fr) are now accessible, Feel free to subscribe by clicking here.
Numerica Ideas
NumericaIdeas is a technical community that creates opportunities for IT professionals, where you’ll secure your dream job, and indeed the group through which you can develop your hard and soft skills at your own pace. Our motto is “Discover, Learn, Build, & Share” 🚀 💡 La chaîne est aussi disponi…

Built-in Env Files Support

Let's do a very simple test by installing the latest NodeJS version as of today (05th of September 2023) and playing with a basic .env file.

Feel free to get NodeJS v20.6.0 from the official download page or by using NVM as we'll in the following lines.

Let's confirm our current NodeJS version by using the command node -v or nvm current:

CurrentNodejsVersion
current nodejs version

Now, we install NodeJS v20.6.0 using the command nvm install 20.6.0:

InstallNodeJS2060
install NodeJS 20.6.0

In case you had it installed already, a simple nvm use 20.6.0 will do.
At the end, we can confirm the installed version is present by using the first command as illustrated above.

We have created a basic .env file with a single line as content:

ACCESS_KEY=FAKE_ACCESS_KEY

Along with a minimalist index.js JS file runnable by NodeJS, here's its content:

console.log(`The access key is: ${process.env.ACCESS_KEY}`)

Both files are very basic and are used for illustration purposes only, so we aren't dealing with a full-fledged NodeJS project but the concept is the same since to access environment variables in NodeJS you'll follow the pattern: process.env.THE_ENVIRONMENT_VAR_KEY.

Finally, let's run the project now, node --env-file=.env index.js:

InstallNodeJS2060
test NodeJS .env files support

As you can see, it simply prints the content of the ACCESS_KEY environment variable, without any dependency installed.

If you are using a version lower than 20.6.0, you'll get the following message since it wasn't included in NodeJS yet:
node: bad option: --env-file=.env

Here's the complete source code of this experimentation and the release notes.

If you are interested in related content, take a look at the following which explains how to Quickly Dockerizing NodeJS Application using docker init command effortlessly:

Docker Init: Quickly Dockerizing NodeJS Application
The docker init command is a powerful tool that enables developers to generate Dockerfiles and Docker Compose files effortlessly.

———————

We have just started our journey to build a network of professionals to grow even more our free knowledge-sharing community that’ll give you a chance to learn interesting things about topics like cloud computing, software development, and software architectures while keeping the door open to more opportunities.

Does this speak to you? If YES, feel free to Join our Discord Server to stay in touch with the community and be part of independently organized events.

———————

Conclusion

In this article, we have seen how NodeJS +v20.6.0 supports .env files out of the box without any third-party dependency.

Thanks for reading this article. Like, recommend, and share if you enjoyed it. Follow us on Facebook, Twitter, and LinkedIn for more content.