DEV Community


Posted on • Originally published at

NodeJS ENV Support

Starting from Node.js v20.6.0, Node.js supports .env files for configuring environment variables.

Previously you would need to use a package called dotenv to load environment variables from a .env file into process.env.

This is now built into Node.js and you can use the --env-file flag to load a .env file into your application.


The .env file should be in the root of your project and should contain the environment variables you want to load into your application.

Your configuration file should follow the INI file format, with each line containing a key-value pair for an environment variable.

Enter fullscreen mode Exit fullscreen mode


To initialize your Node.js application with predefined configurations, use the following CLI command: node --env-file=config.env index.js.

For example, you can access the following environment variable using process.env.PASSWORD when your application is initialized.

const password = process.env.PASSWORD
Enter fullscreen mode Exit fullscreen mode


It's a good idea to include a .env.example file in your project, this will contain all the environment variables that are required for your application to run.

This will allow you to see what environment variables are required and what values they should be.


In addition to environment variables, this change allows you to define your NODE_OPTIONS directly in the .env file, eliminating the need to include it in your package.json.

Enter fullscreen mode Exit fullscreen mode


This is a great addition to Node.js and will make it easier to configure your application when you're running it locally.


  • Node.js v20.6.0
  • dotenv
  • dotenv-safe

Top comments (1)

dotenv profile image

it's amazing to see Node adopt .env! as it does we are turning our attention to better protecting your .env files. for example, we've created a precommit hook that prevents your .env files from being committed to code: