@browserify/envify

中文文档
6.0.0发布于 2022/10/16MITCommonJS近 30 天下载 5.9万

Selectively replace Node-style environment variables with plain strings.

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. CLI Usage
  4. Module Usage
  5. Purging process.env
  6. Contributors

@browserify/envify Build status stable

Selectively replace Node-style environment variables with plain strings. Available as a standalone CLI tool and a Browserify transform.

Works best in combination with @browserify/uglifyify.

Installation

If you're using the module with Browserify:

npm install @browserify/envify browserify

Or, for the CLI:

sudo npm install -g @browserify/envify

Usage

envify will replace your environment variable checks with ordinary strings - only the variables you use will be included, so you don't have to worry about, say, AWS_SECRET_KEY leaking through either. Take this example script:

if (process.env.NODE_ENV === "development") {
  console.log('development only')
}

After running it through envify with NODE_ENV set to production, you'll get this:

if ("production" === "development") {
  console.log('development only')
}

By running this through a good minifier (e.g. terser), the above code would be stripped out completely.

However, if you bundled the same script with NODE_ENV set to development:

if ("development" === "development") {
  console.log('development only')
}

The if statement will evaluate to true, so the code won't be removed.

CLI Usage

With browserify:

browserify index.js -t @browserify/envify > bundle.js

Or standalone:

envify index.js > bundle.js

You can also specify additional custom environment variables using browserify's subarg syntax, which is available in versions 3.25.0 and above:

browserify index.js -t [ @browserify/envify --NODE_ENV development ] > bundle.js
browserify index.js -t [ @browserify/envify --NODE_ENV production  ] > bundle.js

Module Usage

require('@browserify/envify')

Returns a transform stream that updates based on the Node process' process.env object.

require('@browserify/envify/custom')([environment])

If you want to stay away from your environment variables, you can supply your own object to use in its place:

var browserify = require('browserify')
  , envify = require('@browserify/envify/custom')
  , fs = require('fs')

var b = browserify('main.js')
  , output = fs.createWriteStream('bundle.js')

b.transform(envify({
  NODE_ENV: 'development'
}))
b.bundle().pipe(output)

Purging process.env

By default, environment variables that are not defined will be left untouched. This is because in some cases, you might want to run an envify transform over your source more than once, and removing these values would make that impossible.

However, if any references to process.env are remaining after transforming your source with envify, browserify will automatically insert its shim for Node's process object, which will increase the size of your bundle. This weighs in at around 2KB, so if you're trying to be conservative with your bundle size you can "purge" these remaining variables such that any missing ones are simply replaced with undefined.

To do so through the command-line, simply use the subarg syntax and include purge after envify, e.g.:

browserify index.js -t [ @browserify/envify purge --NODE_ENV development ]

Or if you're using the module API, you can pass _: "purge" into your arguments like so:

b.transform(envify({
    _: 'purge'
  , NODE_ENV: 'development'
}))

Contributors

版本与发布记录

@browserify/envify 共发布 1 个版本,首次发布于 2022/10/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.0.0latest2022/10/164 年前

关于 @browserify/envify 的常见问题

@browserify/envify 是什么?

Selectively replace Node-style environment variables with plain strings.

如何安装 @browserify/envify?

在项目目录运行 npm install @browserify/envify,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 envify,可以用 npx envify 直接运行。

@browserify/envify 支持 TypeScript 吗?

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

@browserify/envify 支持 ESM 还是 CommonJS?

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

@browserify/envify 可以商用吗?

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

@browserify/envify 的下载量有多少?

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

@browserify/envify 还在维护吗?

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