|
| 1 | +# envify # |
| 2 | + |
| 3 | +Selectively replace Node-style environment variables with plain strings. |
| 4 | +Available as a standalone CLI tool and a |
| 5 | +[Browserify](http://browserify.org) v2 transform. |
| 6 | + |
| 7 | +Works best in combination with [uglifyify](http://github.com/hughsk/uglifyify). |
| 8 | + |
| 9 | +## Installation ## |
| 10 | + |
| 11 | +If you're using the module with Browserify: |
| 12 | + |
| 13 | +``` bash |
| 14 | +npm install envify browserify |
| 15 | +``` |
| 16 | + |
| 17 | +Or, for the CLI: |
| 18 | + |
| 19 | +``` bash |
| 20 | +sudo npm install -g envify |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage ## |
| 24 | + |
| 25 | +envify will replace your environment variable checks with ordinary strings - |
| 26 | +only the variables you use will be included, so you don't have to worry about, |
| 27 | +say, `AWS_SECRET_KEY` leaking through either. Take this example script: |
| 28 | + |
| 29 | +``` javascript |
| 30 | +if (process.env.NODE_ENV === "development") { |
| 31 | + console.log('development only') |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +After running it through envify with `NODE_ENV` set to `production`, you'll |
| 36 | +get this: |
| 37 | + |
| 38 | +``` javascript |
| 39 | +if ("production" === "development") { |
| 40 | + console.log('development only') |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +By running this through a good minifier (e.g. |
| 45 | +[UglifyJS2](https://github.com/mishoo/UglifyJS)), the above code would be |
| 46 | +stripped out completely. |
| 47 | + |
| 48 | +## CLI Usage ## |
| 49 | + |
| 50 | +With browserify: |
| 51 | + |
| 52 | +``` bash |
| 53 | +browserify index.js -t envify > bundle.js |
| 54 | +``` |
| 55 | + |
| 56 | +Or standalone: |
| 57 | + |
| 58 | +``` bash |
| 59 | +envify index.js > bundle.js |
| 60 | +``` |
| 61 | + |
| 62 | +## Module Usage ## |
| 63 | + |
| 64 | +**require('envify')** |
| 65 | + |
| 66 | +Returns a transform stream that updates based on the Node process' |
| 67 | +`process.env` object. |
| 68 | + |
| 69 | +**require('envify/custom')([environment])** |
| 70 | + |
| 71 | +If you want to stay away from your environment variables, you can supply |
| 72 | +your own object to use in its place: |
| 73 | + |
| 74 | +``` javascript |
| 75 | +var browserify = require('browserify') |
| 76 | + , envify = require('envify/custom') |
| 77 | + , fs = require('fs') |
| 78 | + |
| 79 | +var bundle = browserify('main.js') |
| 80 | + , output = fs.createWriteStream('bundle.js') |
| 81 | + |
| 82 | +b.transform(envify({ |
| 83 | + NODE_ENV: 'development' |
| 84 | +})) |
| 85 | +b.bundle().pipe(output) |
| 86 | +``` |
0 commit comments