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

@fine-js/env

v1.0.0

Published

Read env vars in a robust way.

Downloads

9

Readme

env

Read environment variables in a robust way and finally stop getting undefineds everywhere across your config.

npm install @fine-js/env

Basic Usage

const env = require('@fine-js/env')

env.has('PORT') // => false
env.get('PORT') // => Error: env var "PORT" missing
env.get('PORT', {required: false, defaultsTo: 3000, parseFn: Number}) // => 3000

console.log(env) // Prints a nice represantation sorted alphabetically by key

API

get(name, options)

Returns the value of variable name or throws an Error based on following options:

  • required bool [true]

    When true, throw in case env var is not set.

  • defaultsTo anything [undefined]

    For non-required env vars, this value will be returned directly without parsing with parseFn.

  • parseFn function [(x) => x]

    Values that come from process.env will be passed to this function for parsing, e.g. for port numbers, you'd want Number; feel free to do any validations here, but note that defaultsTo is returned directly.

  • allowEmpty bool [false]

    By default, empty strings on process.env throw an error; note that setting defaultsTo to '' is okay.

  • allowUndefined bool [false]

    Whether resulting value can be undefined.

  • allowNull bool [false]

    Whether resulting value can be null.

has(name)

Returns true when the variable named name is set to any value.

createEnv()

Default export is an environment object based with the contents of process.env at the time of the module being required. You can get an updated version or create new environment from any other object:

const {createEnv} = require('@fine-js/env')

// Defaults to using process.env at the moment of calling.
createEnv()

// Or you can pass any other object.
createEnv({
  my: 'custom',
  env: '42',
})

Changes

1.0.0

I had no need to change this package for years now, seems like it's time for the first stable version with tests, overhauled API and 0 dependencies 🎉

Env is no longer a class, but a plain object with 4 exports:

  • get is a main function for reading env
  • has will tell you if environment has a certain key
  • keys creates an iterator for all the environment variable names present
  • createEnv allows to create a new environment

Not being a class means no longer inherting from Map, which makes it non-iterable except via keys() (there is rarely a need to iterate over values). Another bonus is being nicely printable with console methods and serializable to JSON.

oneOf option is removed as being out of scope. You'll get better validations and more options by putting those kinds of things under parseFn.

env.get('PORT', {
  required: false,
  defaultsTo: 3000,
  parseFn: (val) => {
    const port = Number(val)
    assert(Number.isSafeInteger(port) && port >= 0 && port <= 65535)
    return port
  }
})

0.0.2

Fixed

  • Small codebase issues and typos.

0.0.1

Added

  • Initial realease.