env

envify

🔧 Selectively replace Node-style environment variables with plain strings.

Showing:

Popularity

Downloads/wk

469K

GitHub Stars

872

Maintenance

Last Commit

4yrs ago

Contributors

10

Package

Dependencies

2

Size (min+gzip)

30.6KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Readme

envify Build Status stable

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

Works best in combination with uglifyify.

Installation

If you're using the module with Browserify:

npm install envify browserify

Or, for the CLI:

sudo npm install -g 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. UglifyJS2), 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 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 [ envify --NODE_ENV development ] > bundle.js
browserify index.js -t [ envify --NODE_ENV production  ] > bundle.js

Module Usage

require('envify')

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

require('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('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 [ 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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

dot
dotenvLoads environment variables from .env for nodejs projects.
User Rating
4.8/ 5
79
Top Feedback
48Easy to Use
38Great Documentation
23Performant
GitHub Stars
14K
Weekly Downloads
21M
ce
cross-env🔀 Cross platform setting of environment scripts
User Rating
4.9/ 5
7
Top Feedback
3Easy to Use
1Great Documentation
1Abandoned
GitHub Stars
6K
Weekly Downloads
4M
nco
nconfHierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
User Rating
4.0/ 5
1
Top Feedback
2Great Documentation
2Easy to Use
1Performant
GitHub Stars
4K
Weekly Downloads
944K
dw
dotenv-webpackA secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
User Rating
4.8/ 5
5
Top Feedback
4Great Documentation
3Easy to Use
3Performant
GitHub Stars
1K
Weekly Downloads
3M
jest-environment-nodeDelightful JavaScript Testing.
User RatingN/A
Top Feedback
N/A
GitHub Stars
37K
Weekly Downloads
14M
See 13 Alternatives

Tutorials

No tutorials found
Add a tutorial