autoenv

1.0.12 • Public • Published

AutoENV

automatically load dotenv files based on NODE_ENV

license release semantic

autoenv uses dotenv to automatically load environment variables from a compatible .env file into process.env.

Install

npm install autoenv

Config

Create a default .env file in the root directory of your project.

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3

Create additional .env.xyz files matching as many environment variants you want:

my_project/
├── .env
├── .env.development
├── .env.staging
└── .env.production

The default .env will always be loaded, if a matching .env.${NODE_ENV} file is present, it will be loaded and overrides the values in .env

Usage

As early as possible in your application, require and configure dotenv.

require('autoenv')

When starting your application, ensure NODE_ENV is set to the environment name you wish to load.

NODE_ENV=staging node index.js
export NODE_ENV=staging
node index.js

Preload

You can use the --require (-r) command line option to preload autoenv. By doing this, you do not need to require in your application code.

node -r autoenv index.js

That's it.

process.env now has the keys and values you defined in your .env file.

const db = require('db')
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
})

Author: Ahmad Nassri • Twitter: @AhmadNassri

Package Sidebar

Install

npm i autoenv

Weekly Downloads

15

Version

1.0.12

License

MIT

Unpacked Size

5 kB

Total Files

5

Last publish

Collaborators

  • ahmadnassri