Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

dotenv

> 安全
Open source

Loads environment variables from .env for nodejs projects.

20.5K stars0 likes6 views
WebsiteGitHub

About

Loads environment variables from .env for nodejs projects.

If you like dotenv, you will probably love dotenvx – for encrypting .env files. Thank you for using dotenv. 🙏

dotenv

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

Watch the tutorial

 

Usage

Install it.

bash
npm install dotenv --save

Create a .env file in the root of your project:

ini
# .env
HELLO="Dotenv"
OPENAI_API_KEY="your-api-key-goes-here"

As early as possible in your application, import and configure dotenv:

javascript
// index.js
require('dotenv').config()
// or import 'dotenv/config' // for esm

console.log(`Hello ${process.env.HELLO}`)
bash
$ node index.js
◇ injected env (2) from .env
Hello Dotenv

That's it. process.env now has the keys and values you defined in your .env file.

 

Advanced

ES6

Import with ES6:

javascript
import 'dotenv/config'

DOTENV_CONFIG_ENCODING, DOTENV_CONFIG_PATH, DOTENV_CONFIG_QUIET, DOTENV_CONFIG_DEBUG, DOTENV_CONFIG_OVERRIDE, DOTENV_CONFIG_SECURE, and DOTENV_CONFIG_FAST provide defaults for config() and dotenv run. Options/flags passed directly take precedence.

bun
bash
bun add dotenv
yarn
bash
yarn add dotenv
pnpm
bash
pnpm add dotenv
deno
bash
deno add dotenv
Monorepos

For monorepos with a structure like apps/backend/app.js, put it the .env file in the root of the folder where your app.js process runs.

ini
# app/backend/.env
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
Multiline Values

If you need multiline variables, for example private keys, those are now supported (>= v15.0.0) with line breaks:

ini
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
...
Kh9NV...
...
-----END RSA PRIVATE KEY-----"

Alternatively, you can double quote strings and use the \n character:

ini
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nKh9NV...\n-----END RSA PRIVATE KEY-----\n"
Comments

Comments may be added to your file on their own line or inline:

ini
# This is a comment
SECRET_KEY=YOURSECRETKEYGOESHERE # comment
SECRET_HASH="something-with-a-#-hash"

Comments begin where a # exists, so if your value contains a # please wrap it in quotes. This is a breaking change from >= v15.0.0 and on.

Parsing

The engine which parses the contents of your file containing environment variables is available to use. It accepts a String or Buffer and will return an Object with the parsed keys and values.

javascript
const dotenv = require('dotenv')
const buf = Buffer.from('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }
Run

Use dotenv run -- to run a command with environment variables from your .env file.

bash
$ dotenv run -- node index.js
◇ injected env (2) from .env

Use -f to select one or more .env files.

bash
$ dotenv run -f .env.local -f .env -- node index.js
◇ injected env (2) from .env.local, .env

Use --quiet to suppress the injected env message.

bash
$ dotenv run --quiet -- node index.js

Use --override to overwrite existing environment variables, and --debug for debug logging.

bash
$ dotenv run --override --debug -- node index.js

Use --secure or config({ secure: true }) to decrypt via dotenvx.

bash
$ npm i @dotenvx/dotenvx
$ dotenv run --secure -- node index.js
javascript
require('dotenv').config({ secure: true })

Or with an environment variable:

bash
$ DOTENV_CONFIG_SECURE=true dotenv run -- node index.js
$ DOTENV_CONFIG_SECURE=true node -e "require('dotenv').config()"

dotenv run --secure resolves local @dotenvx/dotenvx then dotenvx on your PATH. config({ secure: true }) requires a local @dotenvx/dotenvx install.

If your .env contains encrypted: values and you run without --secure / secure: true, dotenv warns and leaves them encrypted.

The same DOTENV_CONFIG_* environment variables formerly used by preload still work with the CLI. CLI flags take precedence.

bash
$ DOTENV_CONFIG_PATH=./.env.local DOTENV_CONFIG_QUIET=true dotenv run -- node index.js

Use --fast (or config({ fast: true })) for the faster character-scanner parser (~2x). Default remains the classic regex parser.

bash
$ dotenv run --fast -- node index.js
javascript
require('dotenv').config({ fast: true })

Supported: DOTENV_CONFIG_PATH, DOTENV_CONFIG_ENCODING, DOTENV_CONFIG_QUIET, DOTENV_CONFIG_DEBUG, DOTENV_CONFIG_OVERRIDE, DOTENV_CONFIG_SECURE, DOTENV_CONFIG_FAST.

Variable Expansion

Use dotenvx for variable expansion.

Reference and expand variables already on your machine for use in your .env file.

ini
# .env
USERNAME="username"
DATABASE_URL="postgres://${USERNAME}@localhost/my_database"
javascript
// index.js
console.log('DATABASE_URL', process.env.DATABASE_URL)
bash
$ dotenvx run --debug -- node index.js
⟐ injected env (2) from .env · [email protected]
DATABASE_URL postgres://username@localhost/my_database
Command Substitution

Use dotenvx for command substitution.

Add the output of a command to one of your variables in your .env file.

ini
# .env
DATABASE_URL="postgres://$(whoami)@localhost/my_database"
javascript
// index.js
console.log('DATABASE_URL', process.env.DATABASE_URL)
bash
$ dotenvx run --debug -- node index.js
⟐ injected env (1) from .env · [email protected]
DATABASE_URL postgres://yourusername@localhost/my_database
Encryption

Use dotenvx for encryption.

Add encryption to your .env files with a single command.

$ dotenvx set HELLO Production -f .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ DOTENV_PRIVATE_KEY_PRODUCTION="<.env.production private key>" dotenvx run -- node index.js
⟐ injected env (2) from .env.production · [email protected]
Hello Production

learn more

Multiple Environments

Use dotenvx to manage multiple environments.

Run any environment locally. Create a .env.ENVIRONMENT file and use -f to load it. It's straightforward, yet flexible.

bash
$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.production -- node index.js
Hello production
> ^^

or with multiple .env files

bash
$ echo "HELLO=local" > .env.local
$ echo "HELLO=World" > .env
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f=.env.local -f=.env -- node index.js
Hello local

more environment examples

Production

Use dotenvx for production deploys.

Create a .env.production file.

bash
$ echo "HELLO=production" > .env.production

Encrypt it.

bash
$ dotenvx encrypt -f .env.production

Set DOTENV_PRIVATE_KEY_PRODUCTION (found in .env.keys) on your server.

$ heroku config:set DOTENV_PRIVATE_KEY_PRODUCTION=value

Commit your .env.production file to code and deploy.

$ git add .env.production
$ git commit -m "encrypted .env.production"
$ git push heroku main

Dotenvx will decrypt and inject the secrets at runtime using dotenvx run -- node index.js.

Syncing

Use dotenvx to sync your .env files.

Encrypt them with dotenvx encrypt -f .env and safely include them in source control. Your secrets are securely synced with your git.

This still subscribes to the twelve-factor app rules by generating a decryption key separate from code.

More Examples

See examples of using dotenv with various frameworks, languages, and configurations.

  • nodejs
  • nodejs (debug on)
  • nodejs (override on)
  • nodejs (processEnv override)
  • esm
  • typescript
  • typescript parse
  • typescript config
  • webpack
  • webpack (plugin)
  • react
  • react (typescript)
  • express
  • nestjs
  • fastify

 

FAQ

Should I commit my `.env` file?

No.

Unless you encrypt it with dotenvx. Then we recommend you do.

What about variable expansion?

Use dotenvx.

Should I have multiple `.env` files?

We recommend creating one .env file per environment. Use .env for local/development, .env.production for production and so on. This still follows the twelve factor principles as each is attributed individually to its own environment. Avoid custom set ups that work in inheritance somehow (.env.production inherits values from .env for example). It is better to duplicate values if necessary across each .env.environment file.

In a twelve-factor app, env vars are granular controls, each fully orthogonal to other env vars. They are never grouped together as “environments”, but instead are independently managed for each deploy. This is a model that scales up smoothly as the app naturally expands into more deploys over its lifetime.

– The Twelve-Factor App

Additionally, we recommend using dotenvx to encrypt and manage these.

How do I use dotenv with `import`?

Import dotenv/config before m

Issues· 1 open

View all issuesOpen on GitHub
  • #1043

    fast parser diverges from the default parser (silent value loss)

    Updated Sep 3, 2026

> Tags

JavaScriptconfiguration-filedotenvenvenvironment-variables

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category安全
PricingOpen source

> Related tools

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.