๐Ÿ”‘ Loads environment variables from .env for nodejs projects with safe

Overview

env-safe

env-safe

CI NPM version NPM Download GitHub contributors

env-safe is module that loads that loads environment variables from a .env file into process.env with type-safe. And can also validate the type of process.env. env-safe is dependent on dotenv and reflect-metadata.

Install

npm intall env-safe --save

Or installing with yarn? yarn add env-safe

Usage

Turn on emitDecoratorMetadata, experimentalDecorators in tsconfig.json:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
    ...
  }
  ...
}

Create a .env file in the root of your project:

DATABASE_HOST="localhost"
DATABASE_PORT=3306

Use env-safe to create config class:

import { Key, Env } from 'env-safe';

@Env()
export class Config {
  @Key()
  static DATABASE_HOST: string;

  @Key()
  static DATABASE_PORT: number;
}

That's it. Just use the newly created config class:

import { Config } from './config.ts';

mysql.connect({
  host: Config.DATABASE_HOST, // String("localhost")
  port: Config.DATABASE_PORT  // Number(3306)
});

Documentation

Comment

Comments may be added to your file on their own line or inline:

# This is a comment
DATABASE_HOST="localhost" # comment
DATABASE_PASSWORD="can-define-with-#"

Comments begin where a # exists, so if your value contains a # please wrap it in quotes.

Default value

Set default value to config class property:

@Env()
export class Config {
  @Key({ default: 'localhost' })
  static DATABASE_HOST: string;

  @Key({ default: 3306 })
  static DATABASE_PORT: number;
}

Type-Safe

Since the provided .env does not contain all the variables defined in config class, an exception is thrown:

DATABASE_HOST=
DATABASE_PORT="wrong data"
@Env()
export class Config {
  @Key()
  static DATABASE_HOST: string; // Not defined Error

  @Key()
  static DATABASE_PORT: number; // NaN Error

  @Key()
  static DATABASE_USER: string; // Not defined Error
}
$ node dist/index.js

ERROR: DATABASE_USER is not defined in .env

Change .env path

Can change .env path in your project:

$ ls
development.env  stagging.env  production.env
@Env({ path: 'development.env' })
export class Config {
  ...
}

Multiple config class

Can define multiple config class:

AWS_SECRET_KEY="secret key"
S3_BUCKET="bucket name"
@Env()
export class AWSConfig {
  @Key()
  static AWS_SECRET_KEY: string; // String("secret key")
}

@Env()
export class S3Config {
  @Key()
  static S3_BUCKET: string; // String("bucket name")
}

Contributing Guide

See CONTRIBUTING.md

You might also like...

Natura is a women-only gym that provides a safe, supportive environment for women of all shapes, sizes, and fitness levels

Natura is a women-only gym that provides a safe, supportive environment for women of all shapes, sizes, and fitness levels

Natura is a women-only gym that provides a safe, supportive environment for women of all shapes, sizes, and fitness levels. Their goal is to help women feel confident and comfortable in their own skin, and to encourage them to lead healthy, active lifestyles.

Nov 30, 2022

mirrord lets you easily mirror traffic from your production environment to your development environment.

mirrord lets you easily mirror traffic from your production environment to your development environment.

mirrord lets you easily mirror traffic from your Kubernetes cluster to your development environment. It comes as both Visual Studio Code extension and a CLI tool.

Dec 24, 2022

A multipurpose transporting app that contains, loads databases, documentation and more.

TransportingApp This project was generated with Angular CLI version 12.0.3. Development server Run ng serve for a dev server. Navigate to http://local

Jan 21, 2022

CLI utility that parses argv, loads your specified file, and passes the parsed argv into your file's exported function. Supports ESM/TypeScript/etc out of the box.

cleffa CLI tool that: Parses argv into an object (of command-line flags) and an array of positional arguments Loads a function from the specified file

Mar 6, 2022

Typescript package compatible with python's pickle loads/dumps

picklefriend Typescript package compatible with python's pickle loads/dumps Installation npm i picklefriend Usage import { pickle } from 'picklefriend

Oct 27, 2022

Experience Lab is a set of utilities that assist in creating instances of Microsoft Energy Data Services, performing data loads, and performing basic management operations.

Experience Lab - Microsoft Energy Data Services Build Status About Experience Lab is an automated, end-to-end deployment accelerator for Microsoft Ene

Dec 14, 2022

Export AWS SSM Parameter Store values in bulk to .env files

aws-parameter-bulk Utility to read parameters from AWS Systems Manager (SSM) Parameter Store in bulk and output them in environment-file or json forma

Oct 18, 2022

Multiple `.env` file supported.

Features Support multiple .env files and keep the inheritance Priority: local not unassigned local mode not unassigned mode e.g. .env.{{mode}}.loc

Oct 31, 2022

Autocompletion, in-code secret peeking ๐Ÿ”Ž, syncing, and more, for your .env files in VSCode. ๐Ÿ‘‘ From the same people who pioneered dotenv.

Autocompletion, in-code secret peeking ๐Ÿ”Ž, syncing, and more, for your .env files in VSCode. ๐Ÿ‘‘ From the same people who pioneered dotenv.

Dotenv Official (with Vault) for VSCode Official Dotenv. Syntax highlighting, autocompletion, in-code secret peeking, and .env file syncing with Doten

Dec 19, 2022
Comments
  • hotfix: ๋นˆ ๋ฌธ์ž์—ด์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋˜ ๋ฌธ์ œ ์ˆ˜์ •

    hotfix: ๋นˆ ๋ฌธ์ž์—ด์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋˜ ๋ฌธ์ œ ์ˆ˜์ •

    ์ฐธ๊ณ ์šฉ PR์ž…๋‹ˆ๋‹ค.

    ์ž‘์—… ๋‚ด์šฉ

    • .env ํŒŒ์ผ์—์„œ ๋นˆ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ์œผ๋กœ ํ—ˆ์šฉ๋˜์ง€ ์•Š๋˜ ๋ฌธ์ œ๋ฅผ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
      • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ž˜๋ชป ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด์„œ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

    ์˜ˆ:

    SENTRY_DSN=""
    
    @EnvSafe()
    class Env {
      @EnvKey()
      static SENTRY_DSN: string;  // โ† ํ—ˆ์šฉ ์•ˆ๋จ! ERROR: SENTRY_DSN is not defined. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•จ! ๐Ÿ˜ก
    }
    

    ์ด์ „ 0.1.x ๋ฒ„์ „์—์„œ๋Š” ์ž˜ ๋™์ž‘ํ•˜๋˜ ๊ธฐ๋Šฅ์ด๋ฉฐ, ์ด๋ฒˆ ๋ฆฌํŒฉํ† ๋ง์„ ํ•˜๋ฉด์„œ ๋™์ž‘ํ•˜์ง€ ์•Š๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    bug fix 
    opened by rhea-so 1
  • [none] refactoring: ์ฝ๊ธฐ ํŽธํ•˜๊ฒŒ ๊ตฌ์กฐ ๊ฐœ์„ , ์†Œ์†Œํ•œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€

    [none] refactoring: ์ฝ๊ธฐ ํŽธํ•˜๊ฒŒ ๊ตฌ์กฐ ๊ฐœ์„ , ์†Œ์†Œํ•œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€

    ์ž‘์—… ๋‚ด์šฉ

    ๊ตฌ์กฐ ๊ฐœ์„ 

    • ํด๋” ๊ตฌ์กฐ๋ฅผ ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.
      • ๊ธฐ์กด: decorators, storages, utils
      • ์‹ ๊ทœ: adapter, decorator, domain, usecase
    • ๋กœ์ง ๋ถ„๋ฆฌ๋ฅผ ํ–ˆ์Šต๋‹ˆ๋‹ค.
      • ๊ธฐ์กด์—๋Š” EnvSafe ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์— ๋ชจ๋“  ๋กœ์ง๋“ค์ด ํ•˜๋“œ์ฝ”๋”ฉ ์‹์œผ๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋“ค์„ src์—์„œ ๋ฐ”๊นฅ์œผ๋กœ ๋นผ๋ƒˆ์Šต๋‹ˆ๋‹ค.
    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋“ค์„ ์ƒˆ๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

    ์†Œ์†Œํ•œ ๊ธฐ๋Šฅ ์ถ”๊ฐ€

    • nullable option์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
    @EnvSafe()
    class Env {
      @EnvKey({ nullable: true })
      static TEST_VALUE: string | null;
    }
    

    .env์— ์—†์œผ๋ฉด null์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

    • ํ•œ๋ฒˆ์— ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ๋‹ค ๋ณด์—ฌ์ฃผ๋„๋ก ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
    image
    • .env ํŒŒ์ผ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ž๋™์œผ๋กœ ๋นˆ ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

    https://user-images.githubusercontent.com/25793226/205256885-1ba27e5d-ec98-4c9a-95be-418a81c28138.mov

    refactoring 
    opened by rhea-so 1
Releases(v0.2.1)
  • v0.2.1(Dec 15, 2022)

  • v0.2.0(Dec 15, 2022)

  • v0.1.4(Sep 23, 2022)

    Changes

    • update: EnvSafe, EnvKey๋กœ ์žฌ๋ช…๋ช… @rhea-so (#2)

    ๐Ÿš€ Features

    • feature: env, key ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์ถ”๊ฐ€ @rhea-so (#1)
    Source code(tar.gz)
    Source code(zip)
Owner
Creatrip
Everyone is a local, Creatrip
Creatrip
Quo is a (free) debugging companion app to help you debug dumped variables, the dumped variables will appear in this Quo client instead of the traditional way which is often tedious.

Quo is a debugging companion to help you debug dumped variables, the dumped variables will appear in this Quo client instead of via the traditional way which is often tedious.

Protoqol 33 Dec 25, 2022
Grupprojekt fรถr kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet fรถr kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide Fรถr information om hur utv

Svante Jonsson IT-Hรถgskolan 3 May 18, 2022
Hemsida fรถr personer i Sverige som kan och vill erbjuda boende till mรคnniskor pรฅ flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo fรถr kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A custom action for setting GitHub Workflow environment variables with YAML configuration files.

yaml-env-action - A custom action for setting GitHub Workflow environment variables with YAML configuration files. Introduction yaml-env-action is a c

Piper Dougherty 3 Dec 13, 2022
Displays environment variables on your Grafana dashboards

Displays environment variables on your Grafana dashboards Introduction The Environment data source is a plugin for Grafana that returns environment va

Volkov Labs 7 Dec 26, 2022
โœจ A tool for versioning, securing and easily sharing environment variables

ev a tool for versioning, securing and easily sharing environment variables initializing โ€ข commands โ€ข using in your project Features โฑ Version control

henrycunh 62 Dec 14, 2022
A package to enable feature-flag support on Next.js via cookies and environment variables

next-feature-flags Add support for feature flags on Next.js based on cookies + environment variables. How it works It reads from cookies and Next.js's

Alexandre Santos 10 Aug 10, 2022
Framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. ๐Ÿ—บ๏ธ Remix driver included. ๐ŸคŸ

About routes-gen is a framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. Think of it as Prisma,

Stratulat Alexandru 192 Jan 2, 2023
Cloudy is a set of constructs for the AWS Cloud Development Kit that aim to improve the DX by providing a faster and type-safe code environment.

cloudy-ts These packages aren't yet published on npm. This is still highly experimental. Need to figure out a few things before releasing the first ve

Cristian Pallarรฉs 5 Nov 3, 2022