All Projects → dbstratta → environment

dbstratta / environment

Licence: MIT License
🌳 Environment variable configuration for Node.js made easy.

Programming Languages

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

Projects that are alternatives of or similar to environment

salak.rs
A multi layered configuration loader and zero-boilerplate configuration parser.
Stars: ✭ 27 (+125%)
Mutual labels:  config, environment, configuration, env
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+17933.33%)
Mutual labels:  config, environment, configuration
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (+508.33%)
Mutual labels:  environment, configuration, variables
dotfiles
My personal app/env configs and dotfiles.
Stars: ✭ 27 (+125%)
Mutual labels:  config, environment, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (+50%)
Mutual labels:  config, environment, configuration
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+1650%)
Mutual labels:  config, environment, configuration
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+15358.33%)
Mutual labels:  config, configuration, env
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 (+500%)
Mutual labels:  config, environment, env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+400%)
Mutual labels:  config, configuration, env
env
A lightweight package for loading OS environment variables into structs for Go projects
Stars: ✭ 24 (+100%)
Mutual labels:  config, configuration, env
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+566.67%)
Mutual labels:  config, configuration, env
spdlog setup
spdlog setup initialization via file configuration for convenience.
Stars: ✭ 68 (+466.67%)
Mutual labels:  config, configuration
profig
Powerful configuration management for Scala (JSON, properties, command-line arguments, and environment variables)
Stars: ✭ 25 (+108.33%)
Mutual labels:  config, configuration
environment-variables
🌳 Provides an abstraction of environment variables.
Stars: ✭ 33 (+175%)
Mutual labels:  environment, variables
ha-config-ataraxis
My Home Assistant Configs. If you like what you see, please ⭐️my repo. It would encourage me a lot 🤘
Stars: ✭ 146 (+1116.67%)
Mutual labels:  config, configuration
neovim-config
Modern NeoVim config for IDE-like development
Stars: ✭ 89 (+641.67%)
Mutual labels:  config, configuration
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (+91.67%)
Mutual labels:  environment, variables
parse it
A python library for parsing multiple types of config files, envvars & command line arguments that takes the headache out of setting app configurations.
Stars: ✭ 86 (+616.67%)
Mutual labels:  config, configuration
laravel-conditional-providers
THIS PACKAGE HAS BEEN DEPRECATED — Load Laravel service providers and facades based on the current environment.
Stars: ✭ 26 (+116.67%)
Mutual labels:  environment, configuration
eslint-define-config
Provide a defineConfig function for .eslintrc.js files
Stars: ✭ 61 (+408.33%)
Mutual labels:  config, configuration

environment

Azure Pipelines npm Codecov David PRs Welcome

Environment variable configuration for Node.js made easy.

The problem

A lot of applications need to ensure that some environment variables are set from the beginning, and checking if the value in process.env is undefined every time is needed gets tedious very fast.

environment allows applications to ensure required env variables are set and are valid according to your definition of valid. See how to use it.

Table of Contents

Installation

With Yarn:

yarn add @strattadb/environment

or with npm:

npm install @strattadb/environment

Usage

An example env.js file:

// env.js

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  nodeEnv: {
    parser: parsers.whitelist(['production', 'development', 'test']),
    required: true,
    envVarName: 'NODE_ENV',
  },
  port: {
    parser: parsers.port,
    required: false,
    defaultValue: 4000,
    envVarName: 'PORT',
  },
});

export default env;

Now in a file:

// otherFile.js

import env from './env';

console.log(env.nodeEnv); // development
console.log(typeof env.nodeEnv); // string

console.log(env.port); // 4000
console.log(typeof env.port); // number

Examples

API

makeEnv(schema: Schema, processEnv?: { [key: string]: string | undefined }): Env

Ensures required env variables are present and returns an env object.

Supports passing a processEnv object as the second argument. If it's not passed, it uses process.env. This object will be used to look up the environment variables.

  • Schema:

    • [key: string]: object - The key will be accessible in the returning env object.
      • parser: function - A function that takes a string and can return anything. The return value will be accesible in env[key]. If the argument is not valid, it should throw.
      • required: boolean - Whether or not the env variable is required. If the value true and the env variable is not set, it'll throw. If the value is false it'll look for the env variable in process.env, if isn't set, it'll use defaultValue.
      • defaultValue: any? - Only valid if required: false. This is the default value of the env variable if it's not set. It won't be parsed or validated.
      • envVarName: string - The name of the env variable to look up (process.env[envVarName]).
      • description: string? - Helper text describing the variable.
  • Env:

    • [key: string]: any - The keys are the same as the ones in the schema.

Parsers

parsers.string(value: string): string

Trivial parser. It doesn't do any validation.

parsers.boolean(value: string): boolean

Ensures the value is a truthy or falsy value.

Truthy values: 'true', '1', 'yes', 'on'.

Falsy values: 'false', '0', 'no', 'off'.

parsers.integer(value: string): number

Ensures the value is an integer.

parsers.float(value: string): number

Ensures the value is a float.

parsers.email(value: string): string

Ensures the value is an email.

parsers.url(value: string): string

Ensures the value is a url.

parsers.ipAddress(value: string): string

Ensures the value is an IP address.

parsers.port(value: string): number

Ensures the value is a port.

parsers.whitelist(whitelistedValues: string[]): Parser<string>

Takes a list of valid values and returns a parser that ensures the value is in the whitelist.

Example:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  color: {
    parser: parsers.whitelilst(['red', 'blue', 'green']),
    required: true,
    envVarName: 'COLOR',
  },
});

parsers.regex(pattern: Regex): Parser<string>

Takes a regex and returns a parser that ensures the value matches the pattern.

Example:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  color: {
    parser: parsers.regex(/^green$/),
    required: true,
    envVarName: 'COLOR',
  },
});

parsers.array<T>({ parser: Parser<T>, separator?: string }): Parser<T>

Takes a parser and returns a parser that parses a list of values. The default value separator is ,.

Example:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  color: {
    parser: parsers.array({ parser: parsers.integer }),
    required: true,
    envVarName: 'FAVORITE_NUMBERS',
  },
});

parsers.positiveInteger(value: string): number

Ensures the value is a positive integer.

parsers.nonPositiveInteger(value: string): number

Ensures the value is a non-positive integer.

parsers.negativeInteger(value: string): number

Ensures the value is a negative integer.

parsers.nonNegativeInteger(value: string): number

Ensures the value is a non-negative integer.

Recipes

Making environment variables required

If required is true and the environment variable isn't set, it'll throw:

// env.js

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  notSet: {
    parser: parsers.string,
    required: true,
    envVarName: 'NOT_SET',
  },
});
node env.js

EnvironmentVariableError: NOT_SET is required but is not set
    at ...
    ...

Specifying a default value for when the env variable is not required

If the env variable is not required you must specify a default value:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  port: {
    parser: parsers.port,
    required: false,
    defaultValue: 4000,
    envVarName: 'PORT',
  },
});

console.log(env.port); // 4000

Providing a custom parser

A parser function must take a string value and can return anything. The value you return is what you'll get in the env object. If the value is not valid you should throw an error:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv({
  someValue: {
    parser: (value) => {
      if (value === 'forbiddenValue') {
        throw new Error('value is forbidden');
      }

      return value;
    },
    required: true,
    envVarName: 'SOME_VALUE',
  },
});

Usage with Dotenv

A common pattern is to load the env variables from a file using dotenv and then ensuring that the required variables are actually present:

const dotenv = require('dotenv');

dotenv.config(); // Loads env variables from `.env` file to `process.env`.

const { makeEnv, parsers } = require('@strattadb/environment');

const env = makeEnv({
  secretToken: {
    parser: parsers.string,
    required: true,
    envVarName: 'SECRET_TOKEN',
  },
});

Providing your own processEnv object

By default, makeEnv uses process.env to look up and get environment variables, but you can pass you own processEnv object as the second argument that will be used instead of process.env:

import { makeEnv, parsers } from '@strattadb/environment';

const env = makeEnv(
  {
    hello: {
      parser: parsers.string,
      required: true,
      envVarName: 'HELLO',
    },
  },
  {
    HELLO: 'WORLD',
  },
);

FAQ

Where should I call makeEnv in my application?

The best place to create the env object is very early in your application startup. Everything before you call makeEnv with your schema will not be guaranteed to have your required env variables.

Does it support changing env variables dynamically?

No, when you create an env object it will read the value of process.env at that time. After that, if anything makes changes to process.env, it will not be reflected in the env object.

Can I use the debug module with environment?

Yes! Set DEBUG=environment.

Can I have more than one env object per application?

Yes! You can have as many env objects as you want!

Node.js support

Node.js version 12 or higher. Every version of this library is tested in Node.js 12, 14 and 16.

Contributing

PRs, feature requests, bug reports, and any kind of contributions are welcome! See CONTRIBUTING.md.

Maintainers

Who's using environment

Related libraries

License

MIT

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].