@4c/env

中文文档
1.2.6发布于 2019/03/09MITCommonJS近 30 天下载 2493

Environment variable management and helpers

原始使用文档
文档目录3 节
  1. Usage
  2. Getting and parsing variables
  3. Loading .env files

@4c/env

A small set of utils for working with environment variables in node and webpack projects. Wraps the excellent getEnv as well

Usage

import * as Env from '@4c/env';

Getting and parsing variables

env wraps and reexports getEnv as get().

Env.get('NODE_ENV'); // 'production'

// Fails hard on missing variables
Env.get('NOT_SET_VAR'); // Error

// Fall back to a default if needed
Env.get('NOT_SET_VAR', 'default_value'); // 'default_value'

// Parsing
Env.get.int('MAX_SUBSCRIPTIONS', 4);

process.env.BOOLISH = 1;
Env.get.boolish('BOOLISH'); // true

Loading .env files

env will load and parse .env files, by environment. We use the naming convention variables-{ENV}.env to split out variables by different environment. The default is dev.

Env.load(); // adds variables-dev.env to process.env
Env.load('test'); // adds variables-test.env to process.env

There is also a utility for passing enviroment variables to webpack's DefinePlugin allowing instances of process.env.FOO in front-end code to be replaced with a specific compile-time value.

// returns variables-test.env mapped to an object keyed by process.env.[foo] for use in webpack
new webpack.DefinePlugin(Env.define('test'));

版本与发布记录

@4c/env 共发布 10 个版本,首次发布于 2018/01/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.6latest2019/03/097 年前
1.2.52019/03/097 年前
1.2.42019/02/017 年前
1.2.32019/01/257 年前
1.2.22019/01/257 年前
1.2.12019/01/247 年前

关于 @4c/env 的常见问题

@4c/env 是什么?

Environment variable management and helpers

如何安装 @4c/env?

在项目目录运行 npm install @4c/env,或使用 pnpm add / yarn add / bun add。

@4c/env 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/4c__env 是否存在。

@4c/env 支持 ESM 还是 CommonJS?

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

@4c/env 可以商用吗?

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

@4c/env 的下载量有多少?

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

@4c/env 还在维护吗?

最新版本 1.2.6 发布于 2019/03/09(7 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/01/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。