dotenv
dotenv is a zero-dependency module that loads environment variables from a .env file into process.env, following the twelve-factor app methodology for applicati…
Installation
npm install dotenv
yarn add dotenv
pnpm add dotenv
Import
import 'dotenv/config';
Quick Example
// .env file: // DATABASE_URL=postgres://localhost:5432/mydb // API_KEY=sk_test_abc123 import 'dotenv/config'; console.log(process.env.DATABASE_URL); console.log(process.env.API_KEY);
About dotenv
dotenv is a zero-dependency module that loads environment variables from a .env file into process.env, following the twelve-factor app methodology for application configuration. The library reads key=value pairs from a .env file in the project root and makes them available as environment variables without modifying the system environment. dotenv supports variable expansion, multiline values, comments, quoted strings (preserving whitespace in double-quoted values), and export keyword prefixes for shell compatibility. The .env file format has become a universal convention across programming languages and platforms for managing application configuration including database URLs, API keys, service credentials, and feature flags. dotenv is typically loaded as early as possible in the application entry point using require('dotenv').config() or import 'dotenv/config'. The library does not overwrite existing environment variables by default, respecting values already set in the system environment. dotenv-expand extends the library with variable interpolation (referencing other variables within .env files). While modern runtimes like Bun and Deno load .env files natively, dotenv remains essential for Node.js applications and is used by virtually every Node.js project that needs environment-based configuration.
Quick Facts
| Package | dotenv |
| Category | Utility |
| Weekly Downloads | 35M+ |
| License | BSD-2-Clause |
| Install | npm install dotenv |
Related Packages
cross-env is a utility that allows you to set environment variables inline in npm scripts in a way t…
Commander is the most popular library for building command-line interfaces in Node.js, providing a f…
debug is a tiny JavaScript debugging utility modeled after Node.js core's debugging technique, provi…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.