Node.js version 20.6 now includes built-in support for .env files, which allows developers to load environment variables without using third-party packages. This is a fantastic addition and makes life easier; however, it's essential to be mindful of some limitations.
Let's take a look at how to utilize this new feature. If you have Node 20.6 installed, you can create an .env file like such:
SITE_ID="1233456"
SITE_URL="URL"
Then, type the following command in your terminal or command prompt:
node --env-file .env index.js
You can now access the environment configurations and secrets defined in your .env files within your code.
// index.js
console.log(`Hello ${process.env.SITE_URL}`)
// URL
Need to switch between different production configurations for your JavaScript projects? You can do so by creating new files that point to specific .env files (such as .env.production).
The order in which you run commands is important
There's a small detail to remember when executing the script: the .env file should be passed in before mentioning the file name. Ideally, it would be interchangeable, but for now, this is not possible. If you don't follow this sequence, the .env file will be ignored, which may affect your workflow and result in unexpected outcomes.
// .env file gets ignored in this case
node inex.js --env-file .env
Additional considerations
Working with experimental features may come with some caveats. Some of these could potentially lead people to continue using dotenv until support for these gets added. If you encounter any issues due to these missing features, please don't hesitate to follow the GitHub issue to track their progress and offer any feedback or suggestions. Here's a few known caveats:
Multiline support is not available yet
An important consideration is that multiline environment variables are not yet supported. This means that if you attempt to define a multi-line string, it will be considered undefined in your code. Keep an eye on updates and improvements as this feature might be added in future releases.
// .env
WORLD="Hello
World"
// index.js
console.log(`Hello ${process.env.WORLD}`)
// running the script
node --env-file=.env index.js
Hello undefined
Overwitting of values between .env file and environment is possible
Be aware that if you define the same variable in both the .env file and your project's configuration file, it might cause issues as the value from the .env file would overwrite the one defined in the configuration file. This could lead to unexpected behavior or errors in your code.
// .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
Note: There has been a change in version 20.7.0, where values defined in environment variables now take precedence over those defined in configuration files.
No support for variable reference in .env files. This means that you cannot use environment variables as values for other environment variables.
Node currently does not support variable expansion within .env files. As a result, if you try to reference another variable using `$variable` syntax, it will output the variable as a string rather than its value. However, this limitation can be overcome by 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
No support for .env.vault files, which are used to secure sensitive information in .env files.
dotenv-vault, a popular package that enables encryption of secret environment variables, is an alternative to get this support. This package can be particularly useful in production and CI environments.
Conclusion
The support for .env files in Node.js is an exciting development for the Node community. While experimental so far, it holds great potential for streamlining configuration management and making it easier to work with environment variables in your projects. While we hope that this feature will soon become stable and widely adopted, we should also be aware of any possible limitations or changes as the feature progresses through its development stages. Keep an eye on updates and stay prepared for future improvements!
Sources: (1) https://nodejs.org/en/blog/release/v20.6.0/