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/LICENSE2. Maintained
github.com/motdotla/dotenv3. Used by agents' projects
Dependency in 974 public GitHub projects set up for AI coding agents that we scanned (15.6%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
nodejs.org/llms.txtFrom 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
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
Often used with
dotenv in practice
Strengths
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.
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."
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>.
<code>dotenv.parse()</code> accepts a String or Buffer and returns an object, without touching <code>process.env</code>.
Watch out for
<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.
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.
Unquoted values are cut at the first <code>#</code>. The README notes this was a breaking change from <code>>= 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
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
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.
Pick python-dotenv when your app is written in Python rather than JavaScript.
Pick varlock when you want a schema for your .env file that checks each value and keeps secrets away from AI agents.
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.”
Add the package to your project.
npm install dotenv --saveCreate 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'
Rules for your agent
## 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
dotenv 18 removed the preload options and points to its own run command instead.
Modules imported earlier read process.env before dotenv has filled it.
Browser code has no process or file access; Vite already loads .env itself and bundles VITE_ values into public files.
Since version 15 an unquoted
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.