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

envitron

v1.4.1

Published

- Environment manager for node.js - Built on top of Zod used for schema validation

Downloads

327

Readme

Environment-manager

  • Environment manager for node.js
  • Built on top of Zod used for schema validation

How To Use

Schema Based Env Manager

import { createEnvSchema } from "envitron";

const env = await createEnvSchema(
  (z) => {
    return z.object({
      NODE_ENV: z.enum(["development", "production"]),
      DATABASE_URL: z.string().default('postgres://localhost:5432'),
      API_KEY: z.string(),
      DEBUG: z.boolean(),
      EMPTY_VALUE: z.string(),
      QUOTED_EMPTY_VALUE: z.string(),
      SINGLE_QUOTED_EMPTY_VALUE: z.string(),
      SPACED_KEY: z.string(),
      SPACED_KEY_WITH_QUOTES: z.string(),
      SPECIAL_CHARS_IN_VALUE: z.string(),
      TRAILING_SPACES: z.string(),
      LIST_OF_VALUES_WITH_QUOTES: z.array(z.string()),
      LIST_OF_VALUES_WITH_SINGLE_QUOTES: z.array(z.string()),
      LIST_OF_VALUES_WITHOUT_QUOTES: z.array(z.string()),
      OBJECT: z.object({ key: z.string() }),
    });
  },
  {
    logs: false,
    throwErrorOnValidationFail: false,
    rootPath: process.cwd(),
    envFileHierarchy: ['.env'],
  }
);

// Retrieve all the environment variables
const allEnvs = env.getAll();

// Retrieve a specific schema environment variable with a default value, the type will be inferred from the schema
const schemaBasedNodeEnv = env.get('NODE_ENV', "development");

// You can define a default value both in the get() method and in the zod schema (defaultValue in the get() method has the priority)
const databaseUrlWithSchemaDefault = env.get('DATABASE_URL'); // postgres://localhost:5432
const databaseUrlWithLocalDefault = env.get('DATABASE_URL', "postgres://12.12.12.12:5432"); // postgres://12.12.12.12:5432

// Retrieve searching on all the environment variables regardless of the schema
const outsideSchemaEnv = env.get('NON_SCHEMA_ENV');

Schema Less Environment Manager

import { getInstance } from "envitron";

const schemaLessEnvManager = getInstance({
  rootPath: __dirname,
  envFileHierarchy: ['.env'],
});

// Retrieve all the environment variables
const allEnvsSchemaLess = schemaLessEnvManager.getAll();

// Retrieve an env from the environment manager
const nodeEnv = schemaLessEnvManager.get('NODE_ENV', "development");

Env Example

  • To better understand the functionality of the env manager is an example of all handled use cases and resulting values
  • !!! Envs must be defined in a single line regardless of their type !!!
// NUMBERS
PORT=80

// STRINGS 
NODE_ENV=development
DATABASE_URL = " Example "
API_KEY=' 12345 '
DEBUG=true
QUOTED_EMPTY_VALUE=""
SINGLE_QUOTED_EMPTY_VALUE=''
SPACED_KEY = spaced_value
SPACED_KEY_WITH_QUOTES = " spaced_value "
SPECIAL_CHARS_IN_VALUE="!@#$%^&*()_+"
TRAILING_SPACES=trailing_spaces

// EMPTY
EMPTY_VALUE=

// LISTS (must be defined in square brackets with values separated by commas)
LIST_OF_VALUES_WITH_QUOTES=[" example", "example "]
LIST_OF_VALUES_WITH_SINGLE_QUOTES=[' example', "example"]
LIST_OF_VALUES_WITHOUT_QUOTES=[example, example]

// OBJECTS (NOT ADVISED, must be able to pass a JSON.parse call)
OBJECT={"key":"value"}
  • Will be parsed into:
{
  NODE_ENV: 'development',
  DATABASE_URL: ' DAJEEEEE ',
  API_KEY: ' 12345 ',
  DEBUG: 'true',
  SPACED_KEY: 'spaced_value',
  SPACED_KEY_WITH_QUOTES: ' spaced_value ',
  SPECIAL_CHARS_IN_VALUE: '!@#$%^&*()_+',
  TRAILING_SPACES: 'trailing_spaces',
  LIST_OF_VALUES_WITH_QUOTES: [ '0', '1' ],
  LIST_OF_VALUES_WITH_SINGLE_QUOTES: [ ' example', 'example' ],
  LIST_OF_VALUES_WITHOUT_QUOTES: [ 'example', 'example' ],
  OBJECT: { key: 'value' }
}