@basenative/config

中文文档
3.3.0发布于 2026/11/03Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 1914

Type-safe environment configuration loading and validation

原始使用文档
文档目录8 节
  1. Install
  2. Quick Start
  3. With a prefix
  4. With Zod
  5. API
  6. Built-in Validators
  7. Loaders
  8. License

@basenative/config

Type-safe environment configuration with schema validation

Part of the BaseNative ecosystem — a signal-based web runtime over native HTML.

Install

npm install @basenative/config

Quick Start

import { defineConfig, string, number, boolean, oneOf } from '@basenative/config';

const config = defineConfig({
  schema: {
    PORT: number(),
    DATABASE_URL: string(),
    NODE_ENV: oneOf(['development', 'production', 'test']),
    DEBUG: boolean(),
  },
});

console.log(config.PORT);         // number
console.log(config.DATABASE_URL); // string

With a prefix

const config = defineConfig({
  schema: {
    port: number(),
    secret: string(),
  },
  prefix: 'APP_', // reads APP_PORT, APP_SECRET from env
});

With Zod

import { defineConfig, zodAdapter } from '@basenative/config';
import { z } from 'zod';

const config = defineConfig({
  schema: zodAdapter(z.object({
    PORT: z.coerce.number().default(3000),
    DATABASE_URL: z.string().url(),
  })),
});

API

  • defineConfig(options) — Loads and validates config from environment variables.
    • options.schema — A key-to-validator map or a zodAdapter function.
    • options.env — Source object for env vars (defaults to process.env).
    • options.prefix — Optional prefix stripped before matching schema keys.

Built-in Validators

  • string() — Requires a non-empty string value.
  • number() — Coerces the env string to a number; throws if not numeric.
  • boolean() — Coerces 'true'/'1' to true, 'false'/'0' to false.
  • oneOf(values[]) — Requires the value to be one of the listed options.
  • optional(validator) — Wraps any validator to allow missing/undefined values.
  • validateConfig(values, schema) — Validates a plain object against a schema map; throws with actionable messages on failure.

Loaders

  • loadEnv(path?) — Loads a .env file into process.env. Defaults to .env in the working directory.
  • parseEnvFile(content) — Parses a .env file string into a plain object without mutating process.env.
  • zodAdapter(schema) — Adapts a Zod schema for use with defineConfig.

License

Apache-2.0

版本与发布记录

@basenative/config 共发布 14 个版本,首次发布于 2026/03/20;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.3.0latest2026/11/035 天前
3.2.02026/11/035 天前
3.1.02026/11/017 天前
3.0.02026/10/317 天前
2.4.02026/10/2613 天前
2.3.22026/10/2217 天前

关于 @basenative/config 的常见问题

@basenative/config 是什么?

Type-safe environment configuration loading and validation

如何安装 @basenative/config?

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

@basenative/config 支持 TypeScript 吗?

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

@basenative/config 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@basenative/config 可以商用吗?

@basenative/config 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

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

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

@basenative/config 还在维护吗?

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