dotenv-helper

Library for better use of environment variables

Usage no npm install needed!

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

README

Version Dependency Status Downloads npm Github All Releases

dotenv-helper

Library for better use of environment variables.

Argumentation

If you prefer holding environment variables in a file in your root project directory, then you should use dotenv library.

If you want to make CRUD request to this file, then dotenv-helper might be a better choice, than dotenv.

Note

This library contains already the dotenv library, so you shouldn't use both libraries together.

Instructions

  • Run npm i dotenv-helper to install the library

  • Create .env file in your root project directory

  • Write your first environment variables in the .env file, such as:

NODE_ENV=production

PORT=3000

Now you can use those environment variables, such as:

const env = require("dotenv-helper")

const port = env.getEnv("PORT") // => "3000"

But if you need only that, then dotenv library is good enough for you.

Here is what dotenv-helper gives you:

const env = require("dotenv-helper")
let foo = env.getEnv("foo")
if(foo===false){
   foo = "bar"
   env.addEnv("foo","bar").then(console.log)
}

We check if there is a foo enviroinment variable.

If foo is false, this means that there is no such variable in the .env file.

Now we set foo = "bar", as we need that for our code to work, and afterwards we make request to store this in the .env file.

How it works?

First we check if there "foo" environment variable in process.env.

If there is, the library return this value.

If there isn't, then the library checks the content of the .env file

If there is such value, the library returns it.

If there is no such value, the library return null

Special type of variables

Variables of type foo,bar,baz will be considered as array-like. When such varialbes are read, the library will return the array ["foo","bar","baz"]

READ

console.log(env.getEnv("PORT")) //=> "3000"

console.log(env.getEnv("baz")) //=> false

WRITE

You can add variable, if such variable don't exist already

env.addEnv("foo","bar").then(console.log) //=> true as foo didn't exist before

env.addEnv("PORT","3001").then(console.log) //=> false as PORT already exist

EDIT

Editing of variable is not directly possible. You need first to remove the variable, and then add the new value

env.delEnv("PORT").then(()=>{
    env.addEnv("PORT","3001").then(console.log)
})

DELETE

dotenvHelper.delEnv("PORT").then(console.log) //=> true(delete is performed)
dotenvHelper.delEnv("baz").then(console.log) //=> false(no delete as baz wasn't part of .env)

Functions

getEnv(key)string | Array

Takes a key and search for it in process.env

getEnvSecure(key)string | Array

Takes a key and search for .env file It is actually a helper function used for deleting env variable, as process.env is not reliable then

addEnv(key, value)string | Array

Takes a key and search for it in process.env

delEnv(key)Promise

Takes a key and try to remove it from in process.env

getEnv(key) ⇒ string | Array

Takes a key and search for it in process.env

Kind: global function Returns: string | Array - returns null if no match; string if match and array if match with "," inside

Param Type
key string

getEnvSecure(key) ⇒ string | Array

Takes a key and search for .env file It is actually a helper function used for deleting env variable, as process.env is not reliable then

Kind: global function Returns: string | Array - returns null if no match; string if match and array if match with "," inside

Param Type
key string

addEnv(key, value) ⇒ string | Array

Takes a key and search for it in process.env

Kind: global function Returns: string | Array - returns null if no match; string if match and array if match with "," inside

Param Type
key string
value string

delEnv(key) ⇒ Promise

Takes a key and try to remove it from in process.env

Kind: global function Returns: Promise - Promise => boolean depends whether delete operation was perfomed or not

Param Type
key string