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

@flatfile/cross-env-config

v0.0.6

Published

A helper for configuring things in Node or Browser environments

Downloads

322,358

Readme

@flatfile/cross-env-config

@flatfile/cross-env-config is a lightweight, zero-dependency cross-environment configuration registry that works both in Node.js and browser environments. It is designed to provide a consistent way to access environment variables and configuration values across different types of JavaScript environments.

Installation

You can install @flatfile/cross-env-config using npm:

npm install @flatfile/cross-env-config

Or with Yarn:

yarn add @flatfile/cross-env-config

Basic Usage

To use @flatfile/cross-env-config, first import the CrossEnvConfig class:

import { CrossEnvConfig } from '@flatfile/cross-env-config'

You can then use the get method to fetch the value of an environment variable:

const value = CrossEnvConfig.get('MY_ENV_VAR')

This will first check if there are any overrides set for this key, then it will check the attached config registry, the attached config factory, and finally the environment variables.

Setting Overrides

You can set override values for any key using the set method:

CrossEnvConfig.set('MY_ENV_VAR', 'my value')

This value will take precedence over the attached config registry, config factory, and environment variables.

Using a Config Registry

You can attach an object to act as a config registry. This is useful if you want to store your config values somewhere other than environment variables:

const myConfig = {
  MY_ENV_VAR: 'my value',
  ANOTHER_ENV_VAR: 'another value',
}

CrossEnvConfig.attachConfigRegistry(myConfig)

The values in this registry will take precedence over the attached config factory and environment variables, but not over any overrides.

Using a Config Factory

You can attach a function to act as a config factory. This is useful if you need to dynamically generate config values:

CrossEnvConfig.attachConfigFactory((key) => {
  return `Value for ${key}`
})

The values produced by this factory will take precedence over the environment variables, but not over any overrides or the attached config registry.

Using Aliases

If you have different naming constructs for different environments, you can use the alias method to map one key to another:

CrossEnvConfig.alias('MY_ENV_VAR', 'MY_ALIAS')

In this case, if CrossEnvConfig.get('MY_ALIAS') is called and no value is found for 'MY_ALIAS' in the override, registry, factory, or environment, it will return the value of 'MY_ENV_VAR'.

Safety in Browsers and Node.js

@flatfile/cross-env-config is designed to work safely in both browser and Node.js environments. It checks the type of the process variable before attempting to access process.env, so it won't cause errors in a browser environment where process.env is undefined. It also checks the type of its registry and factory before trying to use them, so it won't break if they aren't properly set.