Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
@flatfile/cross-env-config
Advanced tools
A helper for configuring things in Node or Browser environments
@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.
You can install @flatfile/cross-env-config
using npm:
npm install @flatfile/cross-env-config
Or with Yarn:
yarn add @flatfile/cross-env-config
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.
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.
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.
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.
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'.
@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.
FAQs
A helper for configuring things in Node or Browser environments
The npm package @flatfile/cross-env-config receives a total of 47,631 weekly downloads. As such, @flatfile/cross-env-config popularity was classified as popular.
We found that @flatfile/cross-env-config demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 27 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.