Finally you can skip dotenv in Node
Tags: node, javascript, npm
Published: 2023-09-09 07:33:31

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:

```ini
DB_URL=foo
JWT_SECRET=foo
LOG_LEVEL=debug
```

## Running your Application

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

```bash
node --env-file=config.env index.js
```

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:

```javascript
const DB_URL = process.env.DB_URL;
```

## 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:

```ini
NODE_OPTIONS=--experimental-modules --inspect
LOG_LEVEL=debug
```

## 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.