Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 4 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

dotenv

Keep keys and settings in a .env file.

Dev workflow and configNPM

GitHubWebsite

1. License

“Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met”

BSD-2-Clause allows use in paid and closed-source apps.

github.com/motdotla/dotenv/blob/master/LICENSE

From the package registry and GitHub, checked daily; card checked Nov 10, 2026. Tap a verdict to see its source.How we checkedReport an error

What dotenv does

dotenv is a small free package for Node.js apps. It reads a plain text file called .env, with lines like PORT=8080, and hands those values to your server code when it starts. Passwords and service keys stay out of the code itself, and each computer or server keeps its own file.

What people build with it

Keep service keys out of code

Your assistant puts the database address and service keys in .env, and the code only reads their names, so the secrets never land in your shared code.

Start any command with your settings

Run a script, a test or a dev server with the values from .env loaded, without changing a line of its code.

Separate settings for your laptop and the server

Keep one file per place the app runs, for example .env.local for your own machine and .env on the server, and load them in a fixed order.

Read a settings file into an object

An admin tool or a check before deploy reads a .env file and lists its keys, without changing the running app's settings.

Test settings that never touch production Idea

Your test setup loads a .env.test file into its own object, so a test run cannot pick up the real database address by mistake.

dotenv popularity

86 / 100
Gid Score, #25 overall
288M
downloads a week, npm
20.6K
GitHub stars
974
agent projects use it
dotenv: downloads per weeknpm, dotenv 287,688,164
dotenv: GitHub starsmotdotla/dotenv 20,572

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How dotenv is used

  • dotenv is a dependency in 974 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (20.0% of the 4,867 JavaScript/TypeScript ones), #1 of 299 in dev workflow and config.
  • Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 3,987; those are left out of every share on this page.
  • Projects that use it most often also use tsx (43.8%), TypeScript (83.3%), Zod (56.6%).
  • It is more common in projects set up for Devin Desktop (25.8%) than for GitHub Copilot (15.6%).

By coding agent

515 projects, 15.7%
472 projects, 15.6%
435 projects, 17.8%
89 projects, 18.8%
78 projects, 16.6%
72 projects, 25.8%
65 projects, 19.5%
60 projects, 17.5%

Often used with

In 43.8% of its projects
In 83.3% of its projects
In 56.6% of its projects
In 26.9% of its projects
In 19.0% of its projects
In 50.4% of its projects
In 38.3% of its projects
In 48.4% of its projects

dotenv in practice

Strengths

One line of setup, no dependencies

The README describes it as zero-dependency. Setup is a single <code>config()</code> call or <code>import 'dotenv/config'</code> placed early in the app.

<code>dotenv run</code> needs no code changes

The CLI injects variables into any command. <code>-f</code> loads several files in order, and <code>-q</code> hides the "injected env" message. The README pitches the CLI as "Great for your coding agent."

Existing variables are left alone

If a key in <code>.env</code> is already set in the environment, dotenv skips it. You have to opt in to replacing it with <code>override: true</code> or <code>--override</code>.

The parser can be called on its own

<code>dotenv.parse()</code> accepts a String or Buffer and returns an object, without touching <code>process.env</code>.

Watch out for

No variable expansion or command substitution

<code>${VAR}</code> references and <code>$(cmd)</code> output are not handled by dotenv. The README sends both to dotenvx. Its plugin FAQ shows <code>dotenv-expand</code> applied to the object that <code>config()</code> returns.

No encryption, so keep <code>.env</code> out of git

The FAQ says not to commit <code>.env</code> unless you encrypt it with dotenvx. On the website, the creator calls lack of encryption the format's "biggest security flaw" and recommends dotenvx.

<code>#</code> starts a comment unless quoted

Unquoted values are cut at the first <code>#</code>. The README notes this was a breaking change from <code>&gt;= v15.0.0</code>.

Load .env as early as possible

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

console.log(`Hello ${process.env.HELLO}`)

More about dotenv

Funding
  • GitHub Sponsors: https://github.com/motdotla
  • Own page: https://www.dotenvx.com
  • 4 GitHub sponsors

See all

Packages around it
  • express: Web framework
  • tsx: TypeScript runner
  • ts-node: TypeScript runner
  • zod: Schema validation
  • cors: CORS middleware
  • axios: HTTP client
More in Dev workflow and config

Alternatives

dotenv This page

BSD-2-ClauseGid 86288M/wk

dotenvx

Pick dotenvx when you want encrypted .env files you can keep in git, variable expansion or many environments; the dotenv README points there for these.

python-dotenv

Pick python-dotenv when your app is written in Python rather than JavaScript.

Varlock

Pick varlock when you want a schema for your .env file that checks each value and keeps secrets away from AI agents.

Compare dotenv with another tool

Get started

Tell your AI assistant

“Use the npm package dotenv (pin dotenv@18.0.10) to load settings from .env at server start with import 'dotenv/config' as the first import, add .env to .gitignore, and read keys only from process.env.”

  1. Add the package to your project.

    npm install dotenv --save

    From github.com/motdotla/dotenv/blob/master/README.md

  2. Create a .env file in the project folder with your settings, one per line.

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

    From github.com/motdotla/dotenv/blob/master/README.md

  3. Load it as the very first thing your app does.

    // index.mjs (ESM)
    import 'dotenv/config'
    import express from 'express'
    

    From github.com/motdotla/dotenv/blob/master/README.md

Rules for your agent

Paste into AGENTS.md or CLAUDE.md
## dotenv
- Call `require('dotenv').config()` or `import 'dotenv/config'` before any module that reads `process.env`. If you pass options and imported modules read variables when they load, put `config()` in a separate wrapper file imported first.
- To inject variables without editing code, run `dotenv run -- node index.js`.
- Do not commit `.env`. Keep one file per environment (`.env`, `.env.production`) and do not make files inherit from each other.
- dotenv does not overwrite variables that are already set. Pass `override: true` or `--override` only when that is intended.
- Wrap values containing `#` in quotes. Use quoted multiline values or `\n` for private keys.
- Do not use `${VAR}` or `$(cmd)` in `.env` and expect dotenv to expand them. The README sends that to dotenvx, and its plugin FAQ shows `dotenv-expand` for expansion.

Mistakes to avoid

node -r dotenv/config app.js dotenv_config_path=.env.local (the old preload options)
dotenv run -f .env.local -- node app.js (or DOTENV_PATH=.env.local)

dotenv 18 removed the preload options and points to its own run command instead.

import express from 'express' first, then import 'dotenv/config'
import 'dotenv/config' as the very first import, or a load-env.mjs file imported first

Modules imported earlier read process.env before dotenv has filled it.

Adding dotenv to a Vite or React front-end and reading process.env in browser code
In a Vite app use import.meta.env and the VITE_ prefix only for values that may be public; keep secrets on the server

Browser code has no process or file access; Vite already loads .env itself and bundles VITE_ values into public files.

SECRET_HASH=something-with-a-#-hash (unquoted)
SECRET_HASH="something-with-a-#-hash"

Since version 15 an unquoted

Expecting .env to replace a value that is already set in the shell or by the host
Leave it (host settings should win in production), or pass config({ override: true }) on purpose

Values that already exist are skipped by default.

Questions

Should I commit my .env file?

No. Keep it out of git; the README makes an exception only for files encrypted with dotenvx.

Why are my values undefined in an ES module app?

Imports run before the rest of your file, so import 'dotenv/config' must be the first import, before modules that read the values.

A value is already set on my machine. Which one wins?

The one already set. dotenv never replaces it unless you pass override true.

My value has a

An unquoted

Can I use it in a paid app?

Yes. BSD-2-Clause allows commercial use; keep the copyright notice with copies of the package.