DEV Community

Cover image for Node.js 20.6.0: Say Goodbye to 'dotenv'
Shahid Alam
Shahid Alam

Posted on

Node.js 20.6.0: Say Goodbye to 'dotenv'

Unveiling Node.js 20.6.0

If you're a Node.js developer, you're probably familiar with the 'dotenv' package. It's been a trusty companion for managing environment variables in your Node.js applications. However, with the recent release of Node.js version 20.6.0, managing environment variables just got a whole lot easier, and the need for 'dotenv' is becoming a thing of the past.

What's New in Node.js 20.6.0?

Node.js is constantly evolving, and version 20.6.0 brings some exciting updates. One of the most notable changes is the built-in support for .env files. This means you no longer need to rely on external packages like 'dotenv' to handle your environment variables. Node.js itself can now seamlessly load them from a .env file.

Why Is This a Big Deal?

🔒Enhanced Security: Now you don't have to depend on third-party packages for importing and using .env variables in your code eliminating the risk of any kind of vulnerability.

🌐Streamlined Workflow: With built-in .env file support, your development workflow becomes more straightforward. You don't need to install and configure 'dotenv' or any other third-party packages. Node.js takes care of it for you, reducing the complexity of your project setup(and lighter!).

💼Cleaner and More Maintainable Code: Hardcoding environment variables into your code can make it messy and hard to maintain. It's also a hassle to change them when needed. By using .env files, you keep your codebase clean and make it easier to update configurations as your project evolves.

How to Use .env Files in Node.js 20.6.0

Using .env files in Node.js 20.6.0 is straightforward:

  1. Create a .env File: Start by creating a .env file in the root directory of your project. In this file, you can define your environment variables, each with its corresponding value. For example:
API_KEY=mysecretkey
DATABASE_URL=mongodb://localhost/mydb
DEBUG=true
Enter fullscreen mode Exit fullscreen mode
  1. Run Your Application: To load the environment variables from your .env file, simply run your Node.js application with the --env-file flag, like this:
node --env-file .env
Enter fullscreen mode Exit fullscreen mode

So, why wait? Upgrade to Node.js 20.6.0 and start enjoying the benefits of a cleaner, more secure, and streamlined development process. Your Node.js projects will thank you!

Happy coding! 🚀

Top comments (5)

Collapse
 
khuongduybui profile image
Duy K. Bui

Does it support an env file like this?

DATABASE_NAME=mydb
DATABASE_HOST=localhost
DATABASE_URL=mongodb://$DATABASE_HOST/$DATABASE_NAME
Enter fullscreen mode Exit fullscreen mode
Collapse
 
webjose profile image
José Pablo Ramírez Vargas

Funny how 2 authors made the same article (not same same, but same topic) within the hour.

Anyway, I don't know dotenv, as I evade it like a plague. I created wj-config to be able to use hierarchical configuration files. Your environment names are one underscore away from being compatible with wj-config to produce a configuration object like this:

import { config } from './config.js';

console.log(config.database.name);
console.log(config.database.host);
console.log(config.database.url);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
justaguyfrombr profile image
Just a guy

If you want to configure it with JSON. Why not use the package.json?

Thread Thread
 
leopp33 profile image
Leo Paxtian • Edited

I think, We must be careful with sensitive information, that's the reason We named "secrets.env" in most cases, for example. It's information that doesn't "travel" with application code, in repositories or control version services as Git neither. It must be a singular file that store passwords, server names, database names, specific urls because would be exposed them all.

Otherwise, Do you ask for other reason?
:)

Collapse
 
adaptiveshieldmatrix profile image
Adaptive Shield Matrix

I think with bun 1.0 out -> one can completely ditch nodejs/npm/pnpm/yarn and just use bun.
Bun has support for dotenv and many more features while being 5-10x faster.