Skip to content
uencoder

dotenv

dotenv is the library and convention that loads .env files into environment variables; it is the most popular in the Node.js ecosystem and externalizes configuration.

Purpose

To let local development manage configuration with a .env file while production uses real environment variables, without code changes.

Mechanism

At startup it reads the .env file, parses KEY=VALUE lines, and injects them into process.env. It supports multiple environments (.env.development), variable interpolation ($VAR), and quote handling. It does not override existing environment variables (unless override is set).

Use cases

  • Local configuration for Node.js projects
  • Next.js/Vite environment variables
  • Docker Compose env_file
  • Test environment isolation

Examples

  • Input
    .env: API_KEY=secret
    Output
    process.env.API_KEY="secret"

    Loaded into the environment

Common errors

Loading dotenv in production wastes startup time
Use real environment variables in production and do not load .env

Related terms

Related tools

References