npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dotenv-loader

v2.0.1

Published

Load additional environmental variables from single .env file and manage them on runtime.

Downloads

130

Readme

Dotenv-loader v2.0.1

Load additional environmental variables from single .env file and manage them on runtime. With dotenv-loader you can easily access NodeJS environment variables from different sources.

Description

Variables are available from:

  • Environment created by NodeJS,
  • Set for shell session,
  • Saved in custom .env file,

For what do I need .env file?

It is the easiest way to store all kind of secrets and manage application state. For example when many developers works on one application, everyone can independently defined its own database connection, API keys and other secrets which should not be shared between developers and production.

npm npm node CircleCI Maintenance coverage

Requirements

NodeJS >= 8.9.0

Installation

With NPM:

npm install -S dotenv-loader

For older NodeJS:

npm install -S [email protected]

Usage

Make sure, that dotenv-loader is invoke before main script. Dotenv-loader will parse synchronously your .env file, and set environment variables to process.env array before booting App.

Load environments

env.load([options]);

  • @param options {Object} An optional object with file and encoding
  • @return {EventEmitter} emits 'error' event since v.1.1.0

.env file Example with all recommended formats:

WELCOME_MSG=Hi there!
SINGLE=word
WITH-HYPHEN=foo
WITH.DOT=true
WHITE SPACE=allowed
lowercase=works fine
spaces between = and value

There is no need to put strings in quotes. All quote characters on the right hand side will be included to string itself.

For example:

SECRET = "this is secret string"

Will return as: '"this is secret string"'

At the top of your main script, before application start, call .load() method to set additional environments from .env file.

const
    optionalSettings = {
        file: "./other/path/to/.env", // default: .env
        encoding: "unicode", // default: utf8
    },
    env = require('dotenv-loader');

env.load(optionalSettings).on('error', (err) => console.log(err));

Get environments

env.get(key[, default][, callback]);

  • @param key {String} environment variable key
  • @param default {*} an optional default value if variable was not set
  • @param callback {Function} an optional callback function as last parameter
  • @return {Any} environment value

Optional callback function takes three parameters:

  • @param val {*} value from environment variable
  • @param key {String} variable key
  • @param defaults {*} value passed as default fallback
const
    env = require('dotenv-loader'),
    myVariable = env.get('WELCOME_MSG', 'Hi!');

Throw error if environment variable does not exist

Method .get() takes optional callback as last argument.

let
    val = env.get('NOT_EXIST', 'default val', (val, key, defaults) => {
        console.log(key, val, defaults);
        if (val === null) {
            throw Error('Env variable does not exist');
        }
    });

Full Example

const env = require('dotenv-loader');
env.load().on('error', (err) => throw Error(err));

if (env.get('NODE_ENV') === 'development') {
    console.log('I am in development mode');
}

Changelog

v2.0.1 2018-01-20

Goal: Minor bugs fix.

Patch:

  • Removed unnecessary whitespace replacement on variable parsing,
  • Protection against corrupted .env key=value pair,
  • More test cases for .env parser,

v2.0.0 2018-01-19

Goal: Upgrade to NodeJS 8.x and remove normalization for more flexibility.

Major:

  • Require NodeJS>=8.9,
  • Allow for lower case variable names (will not normalize to upper case),
  • Trim keys and values from .env file on load,

Minor:

  • Allow for spaces in variable keys (will be normalize to underscore),
  • Allow for white spaces before and after equal character for example: ENV_KEY = env value,

Contribution

Did you find any bugs?

Maybe this documentation has language mistakes?

You have idea for great new feature?

Create new issue and describe your point of view. I will do my best to meet all requests.

This repository is open for changes and suggestions. I encourage you to write your own solution and make pull request.

LICENSE

The MIT License (MIT) Copyright (c) 2016 Paweł Zadrożny