1.0.2 • Published 4 years ago

dotera v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

dotera

Features

  • Load .env automatically dependeed on Node Environment
  • Prepend or append strings to keys aur values
  • Lightweight

Install

$ npm install dotera

Usage

At the beginning of your script

require('dotera').load();

Dotera expects .env file to be in the root directory of your project.

Don't worry if you don't want to name your file .env you can rename it and make the following change

require('dotera').load({
  path: '.env.example'
});

Let's suppose you hava a few variables in your .env

DATABASE=demo
USERNAME=admin
PASSWD=secret
# FAKE_VAR="This should not be set"

After loading the initialization script you can access the variables using

process.env.DATABASE
process.env.USERNAME
process.env.PASSWD

Using separate .env files for developemnt and production mode

Let's suppose you have two .env files

.env.production
.env.development

You want to load these variables depended upon your Node ENV. Yes you can switch the different files by manually replaceing the file in the initialization script. But wouldn't be convinient if these files get switched automatically.

Let's see how can we do it.

require('dotera').load({
  dotEnv: '.env.development',
  prodEnv: '.env.production',
});

That's it!

Now the .env files will get swithced depended upon your Node ENV.

Prepending and Appending string to keys and varibles

We can add some environment specific string the variables keys and values.

Suppose we have two databases

dev_database
prod_database

And other a few variables which needes to be prepended or appended on base of Node ENV.

We can do is

require('dotera').load({
  dotEnv: '.env.development',
  prodEnv: '.env.production',
  prepend: {
    val: 'dev_',
  },

  // Other options
  append: {
    key: '_dev',
    val: '_dev',
  }
});

This will return variable

process.env.dev_database_name
1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago