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

import-env

v1.1.1

Published

Easily require and configure env variables in Node.js

Downloads

2

Readme

import-env

An easy way to require and configure env variables in Node.js

NPM version

This is a small helper module built on top of dotenv that allows you to load variables from a .env file and perform additional configuration, such as:

  • Setting default values for variables
  • Marking variables as required
  • Renaming (aliasing) variables

Basic example

// In config.js

const importEnv = require('import-env')

const config = importEnv(
    {
        name: 'PORT',
        default: 4444
    },
    {
        name: 'GITHUB_USERNAME',
        alias: 'USER',
        required: true
    },
    {
        name: 'GITHUB_PASSWORD',
        alias: 'PASS',
        required: true
    }
)

module.exports = config

// Elsewhere in your app

const { PORT, USER, PASS } = require('./config')

Assuming your .env file looks like this:

GITHUB_USERNAME=my_username
GITHUB_PASSWORD=my_password

the above example would return an object that looks like this:

{
    PORT: 4444,                   
    USER: 'my_username',
    PASS: 'my_password'
}

For more information on using .env files, see the docs for dotenv.

Usage

importEnv takes one-to-many arguments, the type of which can be the following:

  • A string name of the variable to load from the .env file (with no associated configuration)
  • A configuration object containing the name of the variable and any additional options

Example with both string and object arguments:

const config = importEnv('API_PRIVATE', 'API_PUBLIC', { name: 'PASSWORD', required: true })

Configuration object API

A configuration object can have the following properties:

  • name (string, required) - The name of the variable to load from the .env file
  • alias (string, optional) - The new name for the variable in the returned config object
  • default (any, optional) - A fallback value for the variable if it isn't set in the .env file
  • required (boolean, optional) - A flag indicating whether to throw an exception if the variable isn't found

Edge cases

What happens when a variable is set as required but a default value is provided?

The variable will fall back to the default value before checking for presence, so the required check will never throw.

What happens when a variable is not required and also not found?

The key will still be in the returned object, but its value will be undefined.

I want to have one of my env variables default to the value of another.

This behavior can get complex as the order of operations may vary, so this function doesn't cover it. You can easily do it outside of the context of the function, though:

const config = importEnv('VALUE', 'DEFAULT_VALUE')
if (!config.VALUE) config.VALUE = config.DEFAULT_VALUE
module.exports = config