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

getenv.ts

v1.4.0

Published

Get and typecast environment variables with TypeScript

Downloads

112

Readme

getenv.ts

Helper to get, validate, cast and provide type safety for environment variables with TypeScript.

Installation

pnpm add getenv.ts
# or
npm install getenv.ts

Usage

Set environment variables in a suitable file, and load them in using a library like dotenv/dotenv-safe, or similar strategy:

export HTTP_HOST="localhost"
export HTTP_PORT=8080
export HTTP_SECURE=true
export AB_TEST_RATIO=0.5

Alternatively, pass the variables along with the execution command:

HTTP_HOST="localhost" HTTP_PORT=8080 node main.ts

Basically, all variables you intend to use must be loaded into process.env in order to be available for getting.

Then you just have to import the package and use the variables:

import getenv from "getenv.ts";

// Get values cast to desired type.
const host: string = getenv.string("HTTP_HOST");
const port: number = getenv.int("HTTP_PORT");
const secure: boolean = getenv.bool("HTTP_SECURE");
const abTestRatio: number = getenv.float("AB_TEST_RATIO");

// Use default value if variable isn't defined.
const sudoUser = getenv.string("SUDO_USER", "root");

// Throws `ReferenceError` when variable isn't defined.
try {
	const invalid = getenv.string("DOES_NOT_EXIST");
} catch (error) {
	// Will throw...
}

// Throws `TypeError` when variable isn't of the expected type.
try {
	const invalid = getenv.int("HTTP_HOST");
} catch (error) {
	// Will throw...
}