dotenv-load

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

Usage no npm install needed!

<script type="module">
  import dotenvLoad from 'https://cdn.skypack.dev/dotenv-load';
</script>

README

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.