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-yaml-safe

v1.0.0

Published

Load environment variables from .env.yml and ensure they are defined

Downloads

3,890

Readme

dotenv-safe

Identical to env-yaml, but ensures that all necessary environment variables are defined after reading from .env.yml. These needed variables are read from .env.example.yml, which should be commited along with your project.

Installation

npm install --save dotenv-yaml-safe
yarn add dotenv-yaml-safe

Example

# .env.example.yml, committed to repo
SECRET:
TOKEN:
KEY:
# .env.yml, private
SECRET: topsecret
TOKEN:
// index.js
require('dotenv-yaml-safe').config();

Since the provided .env.yml file does not contain all the variables defined in .env.example.yml, an exception is thrown:

MissingEnvVarsError: The following variables were defined in .env.example but are not present in the environment:
  TOKEN, KEY
Make sure to add them to .env or directly to the environment.

If you expect any of these variables to be empty, you can use the allowEmptyValues option:
require('dotenv-yaml-safe').config({
  allowEmptyValues: true
});

Not all the variables have to be defined in .env, they can be supplied externally. For example, the following would work:

$ TOKEN=abc KEY=xyz node index.js

Usage

Requiring and loading is identical:

require('dotenv-yaml-safe').config();

This will load environment variables from .env as usual, but will also read any variables defined in .env.example.yml. If any variables are already defined in the environment before reading from .env.yml, they will not be overwritten. If any variables are missing from the environment, a MissingEnvVarsError will be thrown, which lists the missing variables. Otherwise, returns an object with the following format:

{
  parsed: { SECRET: 'topsecret', TOKEN: null },          // parsed representation of .env
  required: { SECRET: 'topsecret', TOKEN: 'external' } // key/value pairs required by .env.example
                                                       // and defined by environment
}

If all the required variables were successfully read but an error was thrown when trying to read the .env.yml file, the error will be included in the result object under the error key.

dotenv-safe compares the actual environment after loading .env.yml (if any) with the example file, so it will work correctly if environment variables are missing in .env.yml but provided through other means such as a shell script.

Options

Same options and methods supported by env-yaml.

require('dotenv-yaml-safe').config({
    allowEmptyValues: true,
    example: './.my-env-example-filename.yml'
});

allowEmptyValues

If a variable is defined in the example file and has an empty value in the environment, enabling this option will not throw an error after loading. Defaults to false.

example

Path to example environment file. Defaults to .env.example.yml.

Credits and Motivation

Based on the work of Robert Wells (env-yaml) and Rodrigo López Dato (dotenv-safe).

I came across dotenv-safe and loved to add it to my existing project but I'm using YAML-based configuration so I ported dotenv-safe using env-yaml as its parser. This is useful for NodeJS apps that can be morphed to use Serverless Framework.

Notable differences agains dotenv-safe

  • Allowed empty values are returned as null instead of empty string