@bedrockio/config

中文文档
4.0.0发布于 2026/07/14MITTypeScriptCommonJS零依赖近 30 天下载 3147

Configurations for node

原始使用文档
文档目录5 节
  1. Install
  2. Philosophy
  3. File cascade
  4. Usage
  5. Methods

@bedrockio/config

Install

npm install @bedrockio/config

Philosophy

Config is loaded when you require this package, not when you launch the process. That is a deliberate choice over Node's native --env-file flags.

--env-file only applies to the one process invocation that remembers to pass it. That splits how a project loads env into two triggers: some files load automatically (whatever a config library reads on import) and some load only when the right flag was on the command line. The failure mode is quiet — a "naked" node script.js still gets base defaults, so it looks like it worked, but silently misses every override the flags would have layered on. Node has also declined to auto-load .env by default, so the per-invocation flag overhead is not going away.

Loading the whole cascade here, on require, removes the split. Every entry point — the server, a worker, a one-off script — resolves config the same way with no flags to remember.

File cascade

Files are merged in order, later files winning:

  1. .env — base defaults, committed.
  2. .env.${ENV_NAME} — per-environment overrides (e.g. .env.production), committed. ENV_NAME is read from the shell, or from the base .env if the shell does not set it.
  3. .env.local — machine-local overrides, gitignored, always wins.

A real process.env value still wins over every file, so ENV_NAME=production node ... both selects the environment and takes precedence. Override files are optional siblings of the base file — with none present, loading is identical to reading .env alone.

All values are loaded into process.env, which is the single source of truth: get/has read from it, and third-party libraries that read process.env directly (Sentry, cloud SDKs, and so on) see your config too. Declaring a key in any file makes it present in the environment; an empty declaration is stored as an empty string, so an override can blank out a base value with KEY=.

Usage

Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:

DB_HOST=localhost
DB_PORT=2000
SENTRY_API=
START_DATE=2019-01-30T10:00:48.185Z
const config = require('@bedrockio/config');
db.connect({
  host: config.get('DB_HOST'),
  port: config.get('DB_PORT', 'number'),
});

// check if START_DATE is bigger than current time and SENTRY_API has value
if (config.get('START_DATE', 'date') < Date.now() && config.has('SENTRY_API')) {
  sentry.init({
    dsn: config.get('SENTRY_API'),
  });
}

Methods

get

Default: config.get('DB_HOST', "string"): string

Return the value of the variable or throws an error if the variable is not defined in the .env or available via process.env.

config.get(
  variable,
  as?: "string" | "boolean" | "json" | "number" | "date"
): string | number | boolean | Date | any

has

Default: config.has('DB_HOST'): boolean

Returns true if the variable is present in process.env (an empty declaration counts as present).

getAll

Default: config.getAll(): {[key: string]: string}

Return all keys declared in your env files as a key/value object, read from process.env. The ambient environment (PATH, etc.) is not included.

getPublic

Default: config.getPublic(): {[key: string]: string}

Like getAll, but only the declared keys whose name starts with a public prefix (APP_ or PUBLIC_ by default, configurable via the ENV_PUBLIC_PREFIXES environment variable, comma separated).

Environment variable ENV_CONFIG_PATH

This variable allows you set a different location for your .env file.

版本与发布记录

@bedrockio/config 共发布 13 个版本,首次发布于 2020/06/05;过去 12 个月发布了 2 个版本。

版本发布日期距今
4.0.0latest2026/07/143 个月前
3.0.02026/01/149 个月前
2.2.32023/06/153 年前
2.2.22022/02/284 年前
2.2.12022/02/284 年前
2.1.12021/07/235 年前

关于 @bedrockio/config 的常见问题

@bedrockio/config 是什么?

Configurations for node

如何安装 @bedrockio/config?

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

@bedrockio/config 支持 TypeScript 吗?

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

@bedrockio/config 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bedrockio/config 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.*。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bedrockio/config 可以商用吗?

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

@bedrockio/config 的下载量有多少?

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

@bedrockio/config 还在维护吗?

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