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

@yummy/dotenv

v5.0.1

Published

A yummy dotenv library

Downloads

306

Readme

A yummy dotenv library

An opinionated .env parsing library. Key features:

  • read .env-files based on NODE_ENV, e.g. .env, .env.local, .env.development
  • pass custom default values for your variables
  • allow / prevent variable inclusion from proces.env
  • use .env.schema to only allow certain values
  • parameter expansion / interpolation of env-variables, e.g. GREET="Hello ${NAME}

Installation

# npm
npm i @yummy/dotenv

# OR yarn
yarn add @yummy/dotenv

Usage

This library makes some assumptions based on available .env-files. Unless configured otherwise, the following load order applies as follows:

  • .env.defaults
  • .env
  • .env.local
  • .env.${NODE_ENV}
  • .env.${NODE_ENV}.local
  • .env.schema
  • process.env

The default options used are as follows:

// for ES6, use `import dotenv from "@yummy/dotenv";`
const dotenv = require("@yummy/dotenv");

const env = dotenv.config({
  // replaces process.env with what is parsed by this library. Set it to true
  // if you want to opt into this.
  override = false

  // the directory to read the .env-files from
  context = path.resolve(process.cwd()),

  // allow system variables to take precedence
  system = true,

  // limit variables to keys specified in here
  schema = '.env.schema',

  // some default values for any environment, good for bootstrapping
  defaults = '.env.defaults',

  // the files to read (in that order)
  files = [
    '.env',
    '.env.local',
    `.env.${NODE_ENV}`,
    `.env.${NODE_ENV}.local`,
  ],
});

.env.defaults

Use this to provide your dotenv config with some default values. Values defined in later files or process.env will override those. If your project does not have this file, then it will be ignored. You can also configure it to disable it explicitly or point it to another defaults file. Alternatively, you may pass an object to defaults.

// the default value
const env = dotenv.config({ defaults: ".env.defaults" });

// point to a different file
const env = dotenv.config({ defaults: "/<path>/<to>/.env.defaults" });

// disable it explicitly
const env = dotenv.config({ defaults: false });

// pass as object, which will NOT attempt to read from file
const env = dotenv.config({
  defaults: { FOO: "default FOO value" },
});

The behaviour is as follows:

// $ cat .env.defaults
// FOO='default foo'
// BAR='default bar'
//
// $ cat .env
// FOO='foo from .env'

const env = dotenv.config();
// => { FOO: 'foo from .env', BAR: 'default bar' }

.env.schema

You may wish to limit your dotenv variables to specific keys only. This is useful if you want to guard your application from an unexpected environment configuration.

// the default value
const env = dotenv.config({ schema: ".env.schema" });

// point to a different file
const env = dotenv.config({ schema: "/<path>/<to>/.env.schema" });

// disable it explicitly
const env = dotenv.config({ schema: false });

The behaviour is as follows:

// $ cat .env.schema
// FOO=
//
// $ cat .env
// FOO='foo from .env'
// BAR='bar from .env'

const env = dotenv.config();
// => { FOO: 'foo from .env'

process.env (system variables)

By default, system variables will be read. They take precedence over and are limited to variables defined in the .env-files. .env.defaults and .env.schema behavour still applies. You can disable this explicitly, of course.

const env = dotenv.config({ system: true });

// disable it
const env = dotenv.config({ system: false });

The behaviour is as follows:

// $ cat .env
// FOO='foo from .env'
// BAR='bar from .env'
//
// $ env
// FOO='foo from system env'
// BAZ='baz from system env' // will be ignored

const env = dotenv.config();
// => { FOO: 'foo from system  env', BAR: 'bar from .env' }

the .env file list

You can also configure the list of .env-files to read. The order of which also determines the read order when parsing those files. In case you pass files that do not exist, they will be ignored. .env.defaults and .env.schema behavour still applies.

// $ cat .env
// FOO='foo from .env'
//
// $ .env.custom
// FOO='foo from custom env'

const env = dotenv.config({ files: [".env", ".env.custom"] });
// => { FOO: 'foo from custom env' }