# dotenv — card for AI agents

Load environment variables from .env files into process.env.

Category: Dev workflow and config · npm package `dotenv` · latest version 18.0.9 (checked 2026-11-01)
Install: `npm install dotenv`
License: BSD-2-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (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.

## Docs written for agents
- [llms.txt (index of the docs)](https://nodejs.org/llms.txt)

## What it is
- dotenv reads a `.env` file and copies its keys into `process.env` in Node.js. The README calls it "a zero-dependency module" and ties the idea of keeping configuration in the environment to The Twelve-Factor App methodology. The repository dates from July 2013 and is BSD-2-Clause licensed.
- There are two ways to use it. You can call `require('dotenv').config()`, or `import 'dotenv/config'` for ESM, as early as possible in your app. Or you can leave your code alone and start it with `dotenv run -- node index.js`. The parser is also exported as `dotenv.parse()`, which takes a String or Buffer and returns an object.
- The creator's website now labels dotenv "Classic – still solid" and recommends dotenvx, which adds encryption. dotenv fits Node projects that only need local variables loaded from a file. Anything beyond that, the README points to dotenvx.

## Caveats
- No variable expansion or command substitution: `${VAR}` references and `$(cmd)` output are not handled by dotenv. The README sends both to dotenvx. Its plugin FAQ shows `dotenv-expand` applied to the object that `config()` returns.
- No encryption, so keep `.env` out of git: The FAQ says not to commit `.env` unless you encrypt it with dotenvx. On the website, the creator calls lack of encryption the format's "biggest security flaw" and recommends dotenvx.
- `#` starts a comment unless quoted: Unquoted values are cut at the first `#`. The README notes this was a breaking change from `>= v15.0.0`.
- Import order matters with ESM: If you pass options to `config()` and other imported modules read variables when they load, the README says to move the call into a separate wrapper file and import that first.
- No layered environment files: The README recommends one `.env` file per environment. It advises against setups where one file inherits from another and says to duplicate values instead.

## Numbers (AgentGid data)
- Gid Score: 86.3 (#25 of all tools, 1 of 299 in its category)
- Used in 974 public projects set up for AI coding agents (15.6%)
- Downloads last week: 287,688,164 (as of 2026-11-24), +43.3% over 4 weeks
- GitHub stars: 20,572 (as of 2026-11-24)
- Last push: 2026-11-09

## Often used with
tsx 43.8%, TypeScript 83.3%, Zod 56.6%, Express 26.9%, pg 19.0%, React 50.4%

## Links
- AgentGid page: https://agentgid.com/tools/dotenv/
- Homepage: https://github.com/motdotla/dotenv#readme
- Repository: https://github.com/motdotla/dotenv
- Source: https://github.com/motdotla/dotenv#readme
- Source: https://github.com/motdotla/dotenv
- Source: https://www.npmjs.com/package/dotenv
- Source: https://www.dotenv.org


## dotenv: keep keys and settings in a .env file

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.

### Where to use 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: 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. (idea, not from the official pages)

### How to start
- Add the package to your project.

```
npm install dotenv --save
```

- 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"
```

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

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


### License
Free to use, also in paid and closed-source apps, under the BSD-2-Clause license. If you hand out copies of the package itself, keep its copyright notice with them.

### Good to know
- Never put the .env file into git. If it happens, remove the file and its history, then add a check that blocks it next time.
- It is for code that runs on a server or your computer. Code in the browser cannot read the file this way.
- When config() runs it prints a short line with how many values it loaded; pass quiet true to hide it. Version 18 prints it to the error stream.
- Values that point to other values, like ${USERNAME}, and encrypted files are not part of dotenv; its README sends you to separate tools for that.
- The project is maintained; version 18.0.10 came out on 8 November 2026.

Checked 2026-11-10 against: https://github.com/motdotla/dotenv/blob/master/README.md, https://github.com/motdotla/dotenv/blob/master/CHANGELOG.md, https://github.com/motdotla/dotenv/blob/master/LICENSE, https://registry.npmjs.org/dotenv/18.0.10, https://vite.dev/guide/env-and-mode

### Mistakes to avoid
- Don't: node -r dotenv/config app.js dotenv_config_path=.env.local  (the old preload options) → Do: 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.)
- Don't: import express from 'express' first, then import 'dotenv/config' → Do: 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.)
- Don't: Adding dotenv to a Vite or React front-end and reading process.env in browser code → Do: 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.)
- Don't: SECRET_HASH=something-with-a-#-hash  (unquoted) → Do: SECRET_HASH="something-with-a-#-hash" (Since version 15 an unquoted)
- Don't: Expecting .env to replace a value that is already set in the shell or by the host → Do: 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.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
