All Projects → evanshortiss → Env Var

evanshortiss / Env Var

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

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Env Var

Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+5695.02%)
Mutual labels:  dotenv, environment, environment-variables
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (-56.72%)
Mutual labels:  dotenv, environment, environment-variables
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (-84.58%)
Mutual labels:  dotenv, environment, environment-variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (-63.68%)
Mutual labels:  dotenv, environment, environment-variables
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (-64.18%)
Mutual labels:  dotenv, environment, environment-variables
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (-91.04%)
Mutual labels:  dotenv, environment, environment-variables
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+976.62%)
Mutual labels:  environment-variables, environment
Dotenv Flow
Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.
Stars: ✭ 537 (+167.16%)
Mutual labels:  environment-variables, dotenv
Envy
Envy automatically exposes environment variables for all of your Go flags
Stars: ✭ 150 (-25.37%)
Mutual labels:  environment, environment-variables
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-29.35%)
Mutual labels:  environment, environment-variables
envfile
Parse and write environment files with Node.js
Stars: ✭ 42 (-79.1%)
Mutual labels:  dotenv, environment-variables
Dotenv Webpack
A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
Stars: ✭ 1,022 (+408.46%)
Mutual labels:  environment-variables, dotenv
React Native Dotenv
Load react native environment variables using import statements for multiple env files.
Stars: ✭ 190 (-5.47%)
Mutual labels:  environment-variables, dotenv
Sync Dotenv
Keep your .env in sync with .env.example
Stars: ✭ 393 (+95.52%)
Mutual labels:  environment-variables, dotenv
Dotenv Kotlin
🗝️ Dotenv is a module that loads environment variables from a .env file
Stars: ✭ 326 (+62.19%)
Mutual labels:  environment-variables, dotenv
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (-88.56%)
Mutual labels:  environment, environment-variables
Variable Injector
Continuous Integration Tool for Swift Projects
Stars: ✭ 63 (-68.66%)
Mutual labels:  environment, environment-variables
Dotenv Java
🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: ✭ 72 (-64.18%)
Mutual labels:  environment-variables, dotenv
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-65.17%)
Mutual labels:  environment, environment-variables
Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (-63.68%)
Mutual labels:  environment, environment-variables

env-var

NPM version TypeScript License Travis CI 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

yarn

yarn add env-var

Getting started

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

Javascript example

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

// Or using import syntax:
// import * as env from 'env-var'

const PASSWORD = env.get('DB_PASSWORD')
  // Throws 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 amdefault value of 5432 if PORT is not defined
const PORT = env.get('PORT').default('5432').asPortNumber()

TypeScript example

import * as env from 'env-var';

// Read a PORT environment variable and ensure it's a positive integer.
// An EnvVarError will be thrown if the variable is not set, or if it
// is not a positive integer.
const PORT: number = env.get('PORT').required().asIntPositive();

For more examples, refer to the /example directory and EXAMPLE.md. A summary of the examples available in /example is written in the 'Other examples' section of EXAMPLE.md.

API

The examples above only cover a very small set of env-var API calls. There are many others 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 'extraAccessors' section of API.md.

Logging

Logging is disabled by default in env-var to prevent accidental logging of secrets.

To enable logging, you need to create an env-var instance using the from() function that the API provides and pass in a logger.

  • A built-in logger is available, but a custom logger is also supported.
  • Always exercise caution when logging environment variables!

Using the Built-in Logger

The built-in logger will print logs only when NODE_ENV is not set to either prod or production.

const { from, logger } =  require('env-var')
const env = from(process.env, {}, logger)

const API_KEY = env.get('API_KEY').required().asString()

This is an example output from the built-in logger generated by running example/logging.js:

logging example output

Using a Custom Logger

If you need to filter env-var logs based on log levels (e.g. trace logging only) or have your own preferred logger, you can use a custom logging solution such as pino easily.

See the 'Custom logging' section of EXAMPLE.md for more information.

Optional integration with dotenv

You can optionally use dotenv with env-var.

There is no coupling between dotenv and env-var, but you can easily use them both together. This loose coupling reduces package bloat and allows you to start or stop using one without being forced to do the same for the other.

See the 'dotenv' section of EXAMPLE.md for more information.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].