Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@humanwhocodes/env

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humanwhocodes/env

A utility to verify that environment variables exist.

  • 0.2.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.1K
decreased by-24.08%
Maintainers
1
Weekly downloads
 
Created
Source

Env utility

by Nicholas C. Zakas

If you find this useful, please consider supporting my work with a donation.

Description

A utility for verifying that environment variables are present in Node.js. The main use case is to easily throw an error when an environment variable is missing. This is most useful immediately after a Node.js program has been initiated, to fail fast and let you know that environment variables haven't been setup correctly.

Folder Structure

The most recent packages are found in these directories:

  • src - the implementation source code
  • tests - tests for the implementation source code

Usage

Node.js

Install using [npm][npm] or [yarn][yarn]:

npm install @humanwhocodes/env --save

# or

yarn add @humanwhocodes/env

Import into your Node.js project:

// CommonJS
const { Env } = require("@humanwhocodes/env");

// ESM
import { Env } from "@humanwhocodes/env";

By default, an Env instance will read from process.env.

Deno

Import into your Deno project:

import { Env } from "https://unpkg.com/@humanwhocodes/env/dist/env.js";

By default, an Env instance will read from Deno.env().

Browser

import { Env } from "https://unpkg.com/@humanwhocodes/env/dist/env.js";

By default, an Env instance will read from an empty object.

API

After importing, create a new instance of Env to start reading environment variables:

const env = new Env();

// read a variable and don't care if it's empty
const username = env.get("USERNAME");

// read a variable and use a default if empty
const username = env.get("USERNAME", "humanwhocodes");

// read a variable and throw an error if it doesn't exist
const username = env.require("USERNAME");

You can also specify an alternate object to read variables from. This can be useful for testing or in the browser (where there is no environment variable to read from by default):

const env = new Env({
    USERNAME: "humanwhocodes"
});

// read a variable and don't care if it's empty
const username = env.get("USERNAME");

// read a variable and throw an error if it doesn't exist
const username = env.require("PASSWORD");

Keywords

FAQs

Package last updated on 25 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc