Add env vars to your process.env
Add or modify variables in your
Often, two processes running at the same time need different environmental variables (for example: running tests and a server from the same gulp process).
gulp-env helps simplify that problem, by letting you establish your env vars whenever you'd like, in a simpler interface. You can set values from an external
.ini, or other file, or programmatically set them directly by using
npm i --save-dev gulp-env
The TypeScript definition file is available in gulp-env.d.ts within the base directory.
// gulpfile.jsvar gulp = ;var nodemon = ;var env = ;gulp;gulp;
ES6 web development:
Simple CoffeeScript library's gulpfile:
gulp = require 'gulp'coffee = require 'gulp-coffee'mocha = require 'gulp-mocha'env = require 'gulp-env'CSON = require 'cson-safe'gulptask 'compile'->gulpsrc'src'pipe coffeepipe gulpdest'dest'gulptask 'test''compile'->gulpsrc'test'pipe envs = envfile: 'config.cson'handler: CSONparsepipe mochapipe envsreset
gulp-env has full test coverage for JSON files, JS modules, INI files, and custom handlers. The entire API below is covered as well. It can also be used in the middle of a Gulp pipeline, where this returns a no-op stream. Note that the
process.env changes happen synchronously, at the time when the function is called.
Read a file and set
process.env accordingly. Both of these forms are equivalent.
Set one or more hardcoded values in
Parse a file, overriding some of its variables.
Parse a file with a custom parser.
Parse a file as a different type.
file option loads the file's contents automatically, calling
require if it isn't a
.ini file or if there is no
handler. You can omit the extension as far as
require allows if it's already registered, since this uses
require under the hood as a fallback.
// .env.jsonMONGO_URI: "mongodb://localhost:27017/testdb"// .env.jsmoduleexports =MONGO_URI: "mongodb://localhost:27017/testdb";// gulpfile.jsvar env = ;processenvMONGO_URI === "mongodb://localhost:27017/testdb"; // maybe false// Any of these will work:; // if the file can be found via `require`;; // if the file can be found via `require`;processenvMONGO_URI === "mongodb://localhost:27017/testdb"; // true
Properties on this object overwrite all existing external properties given by file loading, handlers, etc. All of these will also be added to
// gulpfile.jsvar env = ;;
For the case of just setting environment variables programmatically, you can use
// These two are equivalent. They both can also be used in Gulp streams.;env;
This customizes the parsing of the file. If this is given, the extension name is ignored, and the handler itself is directly called. This is very useful in cases where this module doesn't already support the format. Internally, the module uses this hook for its INI and JSON readers.
The function, if given, is called with two arguments:
contents- the file's contents
filename- the file's name
filenameis still passed, though.
filenamereflects that, i.e. the extension is omitted.
# CSON is frequently used in CoffeeScript projects. Why not use that?env = require 'gulp-env'CSON = require 'cson-safe'envfile: '.env.cson': CSONparse contents
// Or, why can't we use YAML?var env = ;var jsyaml = ;;
Treats the file input as if its extension was
type. It doesn't work for
required files, since Node.js doesn't have hooks to do that, but it currently works for
ini types. Others may potentially be added over time. If you think another one should be added, please, by all means, submit a PR.
var env = ;;// You can also specify it as an extension, as opposed to a type.;
Instances of this interface are returned for
env.set(). These are standard through2 object streams with the following extra methods:
Reset the environment to its former state synchronously. This is designed to be most useful outside of gulpfiles. It returns a boolean, true if any properties were reset, false otherwise. Pass a truthy value as an argument to forcefully restore, i.e. ignore conflicts.
Reset the environment to its former state. Similar to
.restore(), but is called after the incoming stream is flushed, i.e. after all previous Gulp plugins have had their effect on the stream. This is otherwise a no-op through2 object stream. The second version is analogous to
envsreset streamReadable streamWritableenvsresetforce streamReadable streamWritable
Note that such environments can be nested. For example, the following will work:
processenvNODE_ENV // undefinedvar env1 = env;processenvNODE_ENV // "whatever"var env2 = env;processenvNODE_ENV // "something else"env2;processenvNODE_ENV // "whatever"env1;processenvNODE_ENV // undefined
Now, if two settings are restored out of order, conflicting keys (where the currently set value is not the same as the originally set for that version) are simply left as-is. This is the same with externally changed environment variables.
// unbalanced modificationsprocessenvNODE_ENV // undefinedvar env1 = env;processenvNODE_ENV // "whatever"var env2 = env;processenvNODE_ENV // "something else"env1;processenvNODE_ENV // "something else"env2;processenvNODE_ENV // "whatever"// external modificationsprocessenvNODE_ENV // undefinedvar env1 = env;processenvNODE_ENV // "whatever"processenvNODE_ENV = "something else";env1;processenvNODE_ENV // "something else"
Submit a new issue here in the issue tracker
This aims for full test coverage. If you see something missing, please, by all means, send a PR.
To run the tests, run
npm test. The tests and their dependencies are written in