JSPM

  • Created
  • Published
  • Downloads 314890
  • Score
    100M100P100Q171990F
  • License MIT

Verification, sanitization, and type coercion for environment variables in Node.js

Package Exports

  • env-var
  • env-var/env-var.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (env-var) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

env-var

NPM version TypeScript License Coverage Status npm downloads Known Vulnerabilities

Verification, sanitization, and type coercion for environment variables in Node.js and web applications. Supports TypeScript!

  • ๐Ÿ‹ Lightweight. Zero dependencies and just ~4.7kB when minified!
  • ๐Ÿงน Clean and simple code, as shown here.
  • ๐Ÿšซ Fails fast if your environment is misconfigured.
  • ๐Ÿ‘ฉโ€๐Ÿ’ป Friendly error messages and example values for better debugging experience.
  • ๐ŸŽ‰ TypeScript support provides compile time safety and better developer experience.
  • ๐Ÿ“ฆ Support for frontend projects, e.g in React, React Native, Angular, etc.

Contents

Install

npm

npm install env-var --save

yarn

yarn add env-var

Getting started

You can use env-var in both JavaScript and TypeScript!

JavaScript example

const env = require('env-var');

// Import syntax is also supported
// import * as env from 'env-var'

const PASSWORD = env.get('DB_PASSWORD')
  // Throw an error if the DB_PASSWORD variable is not set (optional)
  .required()
  // Decode DB_PASSWORD from base64 to a utf8 string (optional)
  .convertFromBase64()
  // Call asString (or other APIs) to get the variable value (required)
  .asString();

// Read in a port (checks that PORT is in the range 0 to 65535)
// Alternatively, use a default value of 5432 if PORT is not defined
const PORT = env.get('PORT').default('5432').asPortNumber()

TypeScript example

import { get } from 'env-var';

// Read the PORT environment variable. An EnvVarError will be thrown if the
// variable is not set, or if is not in the range 0 to 65535
const PORT: number = get('PORT').required().asPortNumber();

For more examples refer to EXAMPLE.md and the /example directory.

API

env-var supports many accessor functions such as asFloatPositive(), asJson() and asRegExp(). For a full list of env-var API calls, check out API.md.

You can also create your own custom accessor; refer to the usingAccessor section of API.md.

FAQ / Examples

  • Integration with dotenv
  • Logging
  • React
  • Next.js
  • Vite
  • Custom Accessors & Validation Logic

Contributing

Contributions are welcomed and discussed in CONTRIBUTING.md. If you would like to discuss an idea, open an issue or a PR with an initial implementation.

Contributors

  • @aautio
  • @avocadomaster
  • @caccialdo
  • @ChibiBlasphem
  • @DigiPie
  • @evanshortiss
  • @gabrieloczkowski
  • @hhravn
  • @ineentho
  • @itavy
  • @jerome-fox
  • @joh-klein
  • @Lioness100
  • @MikeyBurkman
  • @pepakriz
  • @rmblstrp
  • @shawnmclean
  • @todofixthis
  • @xuo