dotenvx

dotenv NPM version downloads

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.

npm install dotenv --save

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

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

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

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

console.log(`Hello ${process.env.HELLO}`)
$ 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.

 

Agent Usage

Install this repo as an agent skill package:

npx skills add motdotla/dotenv
# ask Claude or Codex to do things like:
set up dotenv
upgrade dotenv to dotenvx

 

Advanced

ES6
Import with [ES6](#how-do-i-use-dotenv-with-import): ```javascript import 'dotenv/config' ``` ES6 import if you need to set config options: ```javascript import dotenv from 'dotenv' dotenv.config({ path: '/custom/path/to/.env' }) ```
bun
```sh bun add dotenv ```
yarn
```sh yarn add dotenv ```
pnpm
```sh pnpm 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' } ```
Preload
> Note: Consider using [`dotenvx`](https://github.com/dotenvx/dotenvx) instead of preloading. I am now doing (and recommending) so. > > It serves the same purpose (you do not need to require and load dotenv), adds better debugging, and works with ANY language, framework, or platform. – [motdotla](https://not.la) You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#-r---require-module) to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. ```bash $ node -r dotenv/config your_script.js ``` The configuration options below are supported as command line arguments in the format `dotenv_config_