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

envaid

v1.0.5

Published

A better way to access your environment variables

Downloads

5

Readme

Envaid

NPM Version Total Downloads

Improve your experience with environment variables using Envaid and its killer features:

  • Fallback: specify a default value to use when a variable does not exist in the environment.
  • Parsing: Automatically parse integers, floats, booleans, JSON objects and arrays.

Envaid is based on Strapi's env-helper.

Getting Started

Getting started with Envaid is super easy.

First, install the envaid package with your favorite package manager.

# If you use NPM
npm install envaid

# If you use Yarn
yarn add envaid

# If you use PNPM
pnpm install envaid

Then, import envaid into your project.

const env = require("envaid");

// Or, if you prefer ES modules

import env from "envaid";

Table of Contents

Documentation

The env function

Retrieves a variable from the environment and returns the variable or defaultValue when the variable is not set.

defaultType can be a variable of any type.

Examples:

// Returns the value of process.env.JWT_SECRET

const jwtSecret = envaid("JWT_SECRET");
// Returns the value of process.env.JWT_SECRET if it is set, or test otherwise

const jwtSecret = env("JWT_SECRET", "test");

The env.int function

Retrieves a variable from the environment and parses it into an integer, or returns defaultValue when the variable is not set.

defaultValue must be either a number or undefined.

Examples:

// Returns the value of MAX_CONCURRENT_PROCESSES as an integer or undefined if the variable is not set

const maxConcurrentProcesses = env.int("MAX_CONCURRENT_PROCESSES");
// Returns the value of MAX_CONCURRENT_PROCESSES as an integer or 2 if the variable is not set

const maxConcurrentProcesses = env.int("MAX_CONCURRENT_PROCESSES", 2);

The env.float function

Retrieves a variable from the environment and parses it into a floating point number, or returns defaultValue when the variable is not set.

defaultValue must be either a number or undefined.

Examples:

// Returns the value of PI_VALUE as a float or undefined if the variable is not set

const pi = env.float("PI_VALUE");
// Returns the value of PI_VALUE as an integer or 3.14 if the variable is not set

const pi = env.float("PI_VALUE", 3.14);

The env.bool function

Retrieves a variable from the environment and parses it into a boolean value or returns defaultValue when the variable is not set.

Values TRUE, true, 1, YES, yes, ON, and on are parsed as true.

Values FALSE, false, 0, NO, no, OFF, and off are parsed as false.

defaultValue must be either a boolean or undefined.

Examples:

// Returns the value of NOTIFICATIONS_ENABLED as a boolean or undefined if the variable is not set

const notificationsEnabled = env.bool("NOTIFICATIONS_ENABLED");
//  Returns the value of NOTIFICATIONS_ENABLED as a boolean or true if the variable is not set

const maxConcurrentProcesses = env.bool("NOTIFICATIONS_ENABLED", true);

The env.json function

Retrieves a variable from the environment and parses it into a JSON object or returns defaultValue when the variable is not set.

defaultValue can be a variable of any type.

Examples:

// Returns the value of CONFIG as a JSON object or undefined if the variable is not set

const config = env.json("CONFIG");
// Returns the value of CONFIG as a JSON object or { foo: "bar" } if the variable is not set

const config = env.json("CONFIG", { foo: "bar" });

The env.array function

Retrieves a variable from the environment and parses it into an array or returns defaultValue when the variable is not set.

Opening and closing brackets, if any, are automatically stripped away from the variables.

defaultValue can be an array of any kind or undefined.

env.array also accepts a trim optional parameter, which defines whether the environment variable and each array element should be trimmed before being returned.

trim is defaulted to true.

Examples:

// Returns the value of APP_KEYS as an array or undefined if the variable is not set

const appKeys = env.json("APP_KEYS");
// Returns the value of APP_KEYS as an array or ["foo", "bar"] if the variable is not set

const appKeys = env.json("APP_KEYS", ["foo", "bar"]);
// Returns the value of process.env.APP_KEYS without trimming it nor the member it parses
// Eg. APP_KEYS = " foo, bar" would return [" foo", " bar"]

const appKeys = env.json("APP_KEYS", undefined, false);