npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@creatrip/env-safe

v0.2.4

Published

🔑 Loads environment variables from .env for nodejs projects with safe

Downloads

40

Readme

env-safe is module 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.

Note: env-safe is not 1.0 yet. Be prepared to do major refactoring due to breaking API changes.

NPM version

Install

npm install @creatrip/env-safe --save

Or installing with yarn? yarn add @creatrip/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 env config class:

import { EnvSafe, EnvKey } from "@creatrip/env-safe";

@EnvSafe()
export class Env {
  @EnvKey()
  static DATABASE_HOST: string;

  @EnvKey()
  static DATABASE_PORT: number;
}

filename: env.ts

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

import { Env } from "./env.ts";

mysql.connect({
  host: Env.DATABASE_HOST, // String("localhost")
  port: Env.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 env config class property:

@EnvSafe()
export class Env {
  @EnvKey({ default: "localhost" })
  static DATABASE_HOST: string;

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

Nullable

Set nullable to env config class property:

@EnvSafe()
export class Env {
  @EnvKey({ nullable: true })
  static DATABASE_HOST: string | null; // String or null
}

Type-Safe

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

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

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

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

ERROR: .env - DATABASE_HOST is not defined
ERROR: .env - DATABASE_PORT is not allowed
ERROR: .env - DATABASE_USER is not defined

Change .env path

Can change .env path in your project:

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

Multiple env config class

Can define multiple env config class:

AWS_SECRET_KEY="secret key"
S3_BUCKET="bucket name"
@EnvSafe()
export class EnvAWS {
  @EnvKey()
  static AWS_SECRET_KEY: string; // String("secret key")
}

@EnvSafe()
export class EnvS3 {
  @EnvKey()
  static S3_BUCKET: string; // String("bucket name")
}

Auto generate template .env

If you don't make .env, just run application. .env will automatically generated by env class:

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

Samples

Take a look at the samples in sample for examples of usage.

Contributing

Learn about contribution here and how to setup your development environment here.

License

env-safe is MIT Licensed.