@flatfile/cross-env-config

中文文档
0.0.6发布于 2024/05/02ISCTypeScriptESM + CJS零依赖近 30 天下载 136万

A helper for configuring things in Node or Browser environments

原始使用文档
文档目录7 节
  1. Installation
  2. Basic Usage
  3. Setting Overrides
  4. Using a Config Registry
  5. Using a Config Factory
  6. Using Aliases
  7. Safety in Browsers and Node.js

@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.

版本与发布记录

@flatfile/cross-env-config 共发布 5 个版本,首次发布于 2023/05/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.6latest2024/05/022 年前
0.0.52023/07/183 年前
0.0.42023/06/073 年前
0.0.32023/05/313 年前
0.0.22023/05/173 年前

关于 @flatfile/cross-env-config 的常见问题

@flatfile/cross-env-config 是什么?

A helper for configuring things in Node or Browser environments

如何安装 @flatfile/cross-env-config?

在项目目录运行 npm install @flatfile/cross-env-config,或使用 pnpm add / yarn add / bun add。

@flatfile/cross-env-config 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flatfile/cross-env-config 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flatfile/cross-env-config 可以商用吗?

@flatfile/cross-env-config 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flatfile/cross-env-config 的下载量有多少?

根据 npm 官方下载统计,@flatfile/cross-env-config 在 2026/10/15 – 2026/11/13 这 30 天内被下载 1,359,720 次,日均约 45,324 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flatfile/cross-env-config 还在维护吗?

最新版本 0.0.6 发布于 2024/05/02(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。