Skip to content

Load environment variables from .env, .env.local, .env.production, etc. when running a npm or yarn command.

Notifications You must be signed in to change notification settings

formatlos/dotenv-load

Repository files navigation

dotenv-load code style: prettier

Load environment variables from .env, .env.local, .env.production, etc. when running a npm or yarn command.

This dotenv loading behaviour was extracted from create-react-app to be also usable with non create-react-app projects like next.js or any other node specific project.

Usage

Use in yarn or npm commands

dotenv-load can be used in an yarn/npm command:

{
  "scripts": {
    "dev": "dotenv-load node server.js",
    "build": "NODE_ENV=production dotenv-load node server.js"
  }
}

Use in node script

dotenv-load can be used in an yarn/npm command:

const dotenvLoad = require('dotenv-load');
dotenvLoad();

ignore .local files

add .local files to .gitignore

.env.local
.env.development.local
.env.production.local

What other .env* files can I use?

dotenv-load will load the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:

  • .env - The Original®
  • .env.local - Local overrides. This file is loaded for all environments except test.
  • .env.development, .env.test, .env.production - Environment-specific settings.
  • .env.development.local, .env.test.local, .env.production.local - Local overrides of environment-specific settings.

How can I use custom env file?

You can pass a string to dotenvLoad function, but your file should have a name like .env.YOUR_CUSTOM_NAME, for example: .env.default.

Then you can initialize it as follows:

dotenvLoad() # will initialize standard names
dotenvLoad('default') # will initialize your `.env.default` (while not overwriting defined vars above)

About

Load environment variables from .env, .env.local, .env.production, etc. when running a npm or yarn command.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •