1.0.0 • Published 1 year ago

dotenv-sebas v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

dotenv-sebas

dotenv is a zero-dependency npm module that loads environment variables from a .env file into process.env.

But in most case, we write some code to load different .env* files in different runtime environments like:

const isDevelopment = process.env.NODE_ENV === 'development';
const isProduction = process.env.NODE_ENV === 'production';

dotenv.config({ path: '.env', override: true });
dotenv.config({ path: '.env.local', override: true });

// load in development envs
if (isDevelopment) {
  dotenv.config({ path: '.env.development', override: true });
  dotenv.config({ path: '.env.development.local', override: true });
}

// load in production envs
if (isProduction) {
  dotenv.config({ path: '.env.production', override: true });
  dotenv.config({ path: '.env.production.local', override: true });
}

dotenv-sebas will help you to handle judge environment and load .env* files like .env.development, .env.test and .env.production, also allowing defined variables to be overwritten individually in the appropriate .env*.local file.

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Why not dotenv-flow ?

dotenv-flow is also a awesome library to do what dotenv-sebas want to do, but it has never been released for two years. And it install dotenv@^8.2.0 as a dependency, which lead to we can't install latest dotenv.

dotenv-sebas reuse a part of dotenv-flow's code, and remove rest part to make self tinier.

Install

npm install dotenv-sebas

Usage

const sebas = require('dotenv-sebas');

sebas.config();

After this, you can access all the environment variables you have defined in your .env* files through process.env.*.

For example, let's suppose that you have the following .env* files in your project:

# .env

DATABASE_HOST=127.0.0.1
DATABASE_PORT=27017
DATABASE_USER=default
DATABASE_PASS=
DATABASE_NAME=my_app
# .env.local

DATABASE_USER=local-user
DATABASE_PASS=super-secret
# .env.development

DATABASE_NAME=my_app_dev
# .env.test

DATABASE_NAME=my_app_test
# .env.production

DATABASE_NAME=my_app_prod
# .env.production.local

DATABASE_HOST=10.0.0.32
DATABASE_PORT=27017
DATABASE_USER=devops
DATABASE_PASS=1qa2ws3ed4rf5tg6yh
DATABASE_NAME=application_storage
// your_script.js

require('dotenv-sebas').config();

console.log('database host:', process.env.DATABASE_HOST);
console.log('database port:', process.env.DATABASE_PORT);
console.log('database user:', process.env.DATABASE_USER);
console.log('database pass:', process.env.DATABASE_PASS);
console.log('database name:', process.env.DATABASE_NAME);

And if you run your_script.js in the development environment, like:

$ NODE_ENV=development node your_script.js

you'll get the following output:

database host: 127.0.0.1
database port: 27017
database user: local-user
database pass: super-secret
database name: my_app_dev

Or if you run the same script in the production environment:

$ NODE_ENV=production node your_script.js

you'll get the following:

database host: 10.0.0.32
database port: 27017
database user: devops
database pass: 1qa2ws3ed4rf5tg6yh
database name: application_storage

Note that the .env*.local files should be ignored by your version control system (refer the Files under version control section below to learn more), and you should have the .env.production.local file only on your production deployment machine.

API reference

sebas.config([options]) => result[]

The main entry point function that load your .env* files.

It returns an array contains every .env* file's load result includes the actual loaded file and the original module (dotenv) returns parsed, error properties.

options.dir

Type: string

Default: process.cwd()

Specify a custom dir path where sebas load your .env* files

options.env

Type: string

Default: process.env.NODE_ENV

Specify a string which environment you are

options.overrideProcessEnv

Type: boolean

Specify whether override the existed key's value in process.env