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 🙏

© 2025 – Pkg Stats / Ryan Hefner

safe-environment-loader

v2.0.1

Published

Safe environment loader

Readme

Safe environment loader

Webpack loader which replaces process.env.<ANYTHING> with environment value. Missing environment values will cause the build error.

Why?

Because you want to be sure that you provide all environment values required by your application.

Installation

npm install safe-environment-loader --save-dev

Usage

Add rule into your webpack config:

{
  enforce: 'post',
  test: /environment\.js/,
  loader: 'safe-environment-loader',
}

Create environment.js:

export default {
  buildId: process.env.BUILD_ID,
  stage: process.env.STAGE
};

Build your application: STAGE=e2e; BUILD_ID=123 webpack -p.

Any missing values required by environment.js will cause build error.

Loader options

Optionally, you can provide the custom environment resolver and filter function.

Environment resolver is a function, which will return the following object or a promise, which resolves into that object:

{
  interface ResolverOutput {
    values?: Record<string, any>;
    error?: Error;
    files?: string[];
  }
}

Filter function works like classic filter function in JS. It receives variable name and value and returns false when substitution needs to be ignored.

{
  enforce: 'post',
  test: /environment\.js/,
  loader: 'safe-environment-loader',
  options: {
    filter: (name, value) => name !== 'IGNORE_ME',
    envResolver: () => ({
      values: {
        BUILD_ID: Math.random(),
      }
    })
  }

CAUTION: process.env.NODE_ENV is always ignored as webpack (since v4) is replacing it by default.

Advanced environment resolver

Environment resolver can implement any complex logic, for example, you can create JS file, which is able to generate ENV values on demand and share it between multiple build processes.

Unfortunately, any changes in that file will be ignored until you restart the Webpack.

To fix that, you can return additional files property to let Webpack know, which files needs to be watched and if any of them change, envResolver will be evaluated again.

// env.config.js

const stage = process.argv[2];

module.exports = () => ({
  API_URL: stage === 'prod' ? 'app.io/api' : 'localhost:3100'
});
// webpack.config.js

{
  enforce: 'post',
  test: /environment\.js/,
  loader: 'safe-environment-loader',
  options: {
    envResolver: () => {
      const resolverFile = require.resolve(`./env.config.js`);
      delete require.cache[resolverFile];
      const config = require(resolverFile)

      try {
        return { values: config(), files: [resolverFile]}
      } catch (error) {
        // We want to recover from any error on next change in `env.config.js`.
        // So, we need to pass `files` even when config evaluation fails.
        return { error, files: [resolverFile] }
      }
    }
  }

With this configuration, Webpack will watch env.config.js for any changes and re-evaluate it when env.config.js or environment.js file change.

Environment value resolution order

  1. check process.env
  2. check envResolver result
  3. throws Missing ENV variable error

According to this resolution order, you can ALWAYS override ENV value by providing it in your terminal: STAGE=dev webpack -p

Advanced usage

💡 We strongly recommend to use envResolver as it is much more flexible.

The following run script will load environment from .env.<stage> file.

#!/usr/bin/env bash

while read assignment; do
  export $assignment
done < $(dirname "$0")/.env.$1

shift 1
"$@"

For example, ./run.sh local webpack -p will load environment form .env.local and then run webpack -p.