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

@bkrmadtya/safe-env

v1.0.4

Published

A thin wrapper around dotenv to make it easy, safe and verbose to access environment variables.

Downloads

10

Readme

safe-env

A thin typescript wrapper around dotenv. Making it safe, easy and verbose to load and use environment variables.

Install

npm install @bkrmadtya/safe-env
# or
pnpm add @bkrmadtya/safe-env
# or
yarn add @bkrmadtya/safe-env

Configuration (optional)

The configuration object is optional. It takes all the same options as dotenv's config function along with an additional message object with notFound and missingRequired methods which can be used to customize the error messages.

import { type Config, loadEnv } from '@bkrmadtya/safe-env';

const config: Config = {
  path: '/path/to/.env',
  ...allDotenvConfigOptions,
  message: {
    notFound: (key: string) =>
      `Variable '${key}' not definded in env schema and env file`,
    missingRequired: (key: string) => `Missing required env variable '${key}'`,
  },
};

Schema

The schema object is used to define the environment variables expected in the application. A default value can be provided for optional variables or a required flag can be passed for variables that are expected to be present in .env file. The same key cannot have both a default value and a required flag.

Incase, a required variable is not found in the .env file or a random key not present in the schema is provided, an error is thrown with a default or a custom message.

Let's take following .env file as an example:

NODE_ENV=production

The schema object for the above .env file can be defined as follows:

const schema = {
  NODE_ENV: { default: 'development' }, // exists in .env
  NAME: { default: 'Ven' }, // doesn't exists
  TEST: { required: true }, // doesn't exist
} satisfies Schema; // 'satisfies' recommended as it provides better ts intellisense

Usage

Recommended usage is to create a env.ts file in the root of your project which can act as a single source of truth for all your environment variables.

import { loadEnv, type Config, type Schema } from '@bkrmadtya/safe-env';

const config: Config = {
  path: '/path/to/.env',
};

const schema = {
  NODE_ENV: { default: 'development' }, // exists in .env with value 'production'
  NAME: { default: 'Ven' }, // doesn't exists
  TEST: { required: true }, // doesn't exist
} satisfies Schema;

const { getEnv } = loadEnv(schema, config);

// returns production
export const NODE_ENV = getEnv('NODE_ENV');

// returns 'Ven'
export const NAME = getEnv('NAME');

// throws error since its required
export const TEST = getEnv('TEST');

// shows ts compile error and throws error at runtime since it doesn't exist in schema
export const RANDOM = getEnv('RANDOM');

env object

The env object from the response dotenv.config() is also returned by the loadEnv function.

import { loadEnv } from '@bkrmadtya/safe-env';

const schema = { ... };

const { env } = loadEnv(schema);

// Do something with env

License

MIT © bkrmadtya 2023