DEV Community

Cover image for Finally you can skip dotenv in Node
Andreas Bergström
Andreas Bergström

Posted on

Finally you can skip dotenv in Node

Node.js has been at the forefront of backend development for over a decade now, largely due to its scalability and efficient performance. Every new release brings with it a myriad of features aimed at simplifying development, and Node.js v20.6 is no exception. One small but highly appreciated feature in this release is the built-in .env file support for configuring environment variables.

What are .env Files?

Environment variables are a crucial part of modern development workflows. They let you store sensitive information, application configurations, and even runtime settings away from your application code. .env files have become the de facto standard for managing these variables. While environment variables are usually injected into production containers, during local development it is common to have the variables in files such as .env.test and .env.development.

How Does it Work?

Prior to v20.6, handling environment variables often involved using external libraries like dotenv, which would parse .env files and inject the variables into process.env. With this new feature, you can directly pass the .env file as an argument to the Node.js runtime.

Syntax

The .env file should be formatted as an INI file, meaning each line contains a key-value pair for an environment variable. For example, your .env file might look something like this:

DB_URL=foo
JWT_SECRET=foo
LOG_LEVEL=debug
Enter fullscreen mode Exit fullscreen mode

Running your Application

You can initialize your Node.js application with predefined configurations using the following CLI command:

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

This will load all the environment variables from config.env into your application's environment. For example, you can now access the password as follows:

const DB_URL = process.env.DB_URL;
Enter fullscreen mode Exit fullscreen mode

NODE_OPTIONS in .env

This new feature doesn't just stop at handling your custom environment variables. It extends to built-in Node.js options as well. Traditionally, defining NODE_OPTIONS would involve including them in your package.json or setting them in your shell before running your application. With this new built-in .env support, you can include NODE_OPTIONS directly in your .env file.

For example, your .env file could contain:

NODE_OPTIONS=--experimental-modules --inspect
LOG_LEVEL=debug
Enter fullscreen mode Exit fullscreen mode

Why Is This Important?

Simplified Workflow
This change simplifies the developer experience by natively incorporating what was already a de facto standard in Node.js development.

Less Dependence on External Packages
By incorporating this feature into the core, Node.js reduces the number of third-party packages that developers need to be familiar with, making it easier to get up and running.

Unified Configuration
This feature also encourages a more unified approach to configuration, bringing environment variables and Node options into a single, easily manageable file.

You might still need 3rd party libraries for frontend

While Node.js 20.6 has streamlined environment variable management with its built-in .env support, it's important to note that this feature doesn't extend to React and React Native. These frameworks don't utilize the Node.js runtime in production and have their own build processes and runtimes. As a result, developers may still need to rely on existing methods for environment variable management. For instance, legacy React projects often make use of tools like create-react-app, which handles .env files in its own way, while React Native developers may turn to libraries such as react-native-config. Any newer projects would probably use Vite, Astro and similiar which has built-in support.

Node.js v20.6's addition of built-in .env file support is more than just a convenience feature; it's a strategic move to maintain its competitive edge in a landscape increasingly influenced by newcomers like Deno and BUN. These emerging technologies have placed a significant emphasis on developer experience right out of the box, pushing Node.js to evolve and incorporate such enhancements natively. This update is not just about streamlining application development in Node.js; it's also about responding to the challenges and setting the bar high in a rapidly evolving ecosystem.

Top comments (8)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

wj-config: The power of .Net configuration for Node and browser projects

Collapse
 
andreasbergstrom profile image
Andreas Bergström • Edited

Interesting, though I can't remember any use case where I wished to pull in env vars from all over the place. For backend services I would probably use a config service instead.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

The package allows you to fetch configuration. When you say "env vars from all over the place", you are thinking environment variables as the only possible configuration source. Your mind is playing you tricks.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

Hehe, no that's why I said config service :). But good look with your package! Perhaps add a section where you compare its features with node-config.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas • Edited

It pretty much does everything node-config can, plus everything dotenv can, plus URL building functions, plus conditional inclusion of data sources. All in half the code. Oh, and works for browser or Node/Deno projects. The only thing I don't provide is a command-line args data source because that's a Node-only thing and the package is meant to work as-is on browsers.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

Cool, does it work with NestJS?

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

Never tried it as I avoid Node for back-end. I always do back-end in C#. However, it seems like one more Node usage to me, so it will most likely work.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

If you are coming from C# I think you'd enjoy NestJS as it's heavily inspired by the .Net web alternatives (full Typescript, using DI etc.), and utilizing decorators a lot. I hated Node backends until I found Nest.