All Projects → LeoBakerHytch → ts-dotenv

LeoBakerHytch / ts-dotenv

Licence: MIT License
Strongly-typed environment variables for Node.js

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to ts-dotenv

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 (+300%)
Mutual labels:  dotenv, environment, environment-variables, env
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (+72.22%)
Mutual labels:  dotenv, environment, environment-variables, env
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-22.22%)
Mutual labels:  environment, environment-variables, env
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (+133.33%)
Mutual labels:  dotenv, environment-variables, 12-factor
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-33.33%)
Mutual labels:  dotenv, environment-variables, env
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+383.33%)
Mutual labels:  dotenv, environment, environment-variables
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (+94.44%)
Mutual labels:  dotenv, environment-variables, env
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+64611.11%)
Mutual labels:  dotenv, environment, environment-variables
envfile
Parse and write environment files with Node.js
Stars: ✭ 42 (+133.33%)
Mutual labels:  dotenv, environment-variables, env
php-env
A small and fast .env loader for PHP
Stars: ✭ 19 (+5.56%)
Mutual labels:  dotenv, environment-variables, env
Env Var
Verification, sanitization, and type coercion for environment variables in Node.js
Stars: ✭ 201 (+1016.67%)
Mutual labels:  dotenv, environment, environment-variables
ngx-env
Easily inject environment variables into your Angular applications
Stars: ✭ 73 (+305.56%)
Mutual labels:  dotenv, environment, environment-variables
webpack-dotenv-plugin
Use dotenv with webpack.
Stars: ✭ 53 (+194.44%)
Mutual labels:  dotenv, environment-variables, env
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (+83.33%)
Mutual labels:  dotenv, environment-variables, 12-factor
rune
tool to query for tokens and passwords for use as environment variables
Stars: ✭ 13 (-27.78%)
Mutual labels:  environment, environment-variables
envman
Manage your .env configuration easily
Stars: ✭ 20 (+11.11%)
Mutual labels:  environment-variables, env
DBEnvironmentConfiguration
Easily switch between iOS development environments/ configurations
Stars: ✭ 18 (+0%)
Mutual labels:  environment, environment-variables
env-dot-prop
♻️ Get, set, or delete nested properties of process.env using a dot path
Stars: ✭ 31 (+72.22%)
Mutual labels:  environment-variables, env
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (+161.11%)
Mutual labels:  dotenv, environment-variables
angular-cli-envvars
Example project for my article "Angular CLI and OS Environment Variables"
Stars: ✭ 56 (+211.11%)
Mutual labels:  dotenv, environment-variables

ts-dotenv

Strongly-typed Node.js environment variables from .env and process.env.

Version License Travis Coveralls Dependabot Status

Motivation

Load environment variables from a .env file for development, but deploy to an environment that injects them directly (on process.env) with no logic needed to differentiate dev from prod. Values from disk are merged with the process environment (you decide whether process.env or .env takes precedence), validated against a simple schema, and coerced to the appropriate types.

ts-dotenv maintains dev/prod parity by not caring whether variables come from .env or process.env, as long as they’re all present and the correct types. Otherwise, it fails fast, so your alarms should start going off and/or your rolling releases will abort. The thrown error details which variables are missing or have the wrong types.

Caution: Be careful removing variables from your prod environment; be sure to first remove them from the schema, otherwise your server won’t boot and it will have nothing to roll back to. (Or you could catch the error ts-dotenv throws, and do your own logging or alerts, but you’ll lose automatic protection from pushing out builds with missing variables. It’s a trade-off.)

Usage

# Comments are supported
TRACING=true
PORT=3000
NODE_ENV=production
APP_NAME=test-app
BASE_URL=https://api.example.com
#BASE_URL=https://development-api.example.com
EXTRA=true
import { strict as assert } from 'assert';
import { load } from 'ts-dotenv';

const env = load({
    TRACING: Boolean,
    PORT: Number,
    APP_NAME: /^[-a-z]+$/,
    BASE_URL: String,
    NODE_ENV: [
        'production' as const,
        'development' as const,
    ],
});

assert.ok(env.TRACING === true);
assert.ok(env.PORT === 3000);
assert.ok(env.APP_NAME === 'test-app');
assert.ok(env.NODE_ENV === 'production');
assert.ok(env.BASE_URL === 'https://api.example.com');
assert.ok(env.EXTRA === undefined);

Note:

  • Number only supports integers
  • Only string unions are supported
  • Use as const with string unions, to ensure a proper resulting environment type

Optionals & defaults

Optional fields and default values can be defined with an extended schema specifier; for example:

const schema = {
    TRACING: {
        type: Boolean,
        optional: true,
    },
    NODE_ENV: {
        type: String,
        default: 'local',
    }
} as const;

Boot

Run ts-dotenv from your app’s entry, to ensure variables are loaded before you wire up services and start serving requests. The following pattern makes for easy, type-safe consumption of variables throughout your app:

index.ts

import { loadEnv } from './env';

loadEnv(); // Executed synchronously before the rest of your app loads

require('./server'); // Your server’s actual entry

env.ts

import { EnvType, load } from 'ts-dotenv';

export type Env = EnvType<typeof schema>;

export const schema = {
    NODE_ENV: String,
};

export let env: Env;

export function loadEnv(): void {
    env = load(schema);
}

example-module.ts

import { env } from './env';

if (env.NODE_ENV === 'production') {
    // ...
}

Options

By default:

  • Values in process.env take precedence
  • .env is the expected file name, loaded from the working directory

Change this through options:

import { resolve } from 'path';
import { load } from 'ts-dotenv';
const env = load(schema, 'lib/.env');
import { resolve } from 'path';
import { load } from 'ts-dotenv';

const env = load(schema, {
    path: resolve(__dirname, '.env'),
    encoding: 'iso-8859-1',
    overrideProcessEnv: true,
});

Improvements

  • Add support for floats
  • Add support for number union types

Acknowledgements

This was inspired by dotenv and dotenv-extended, but written for first-class use in a TypeScript project.

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