Notes

Browserify, Mochify, Nyc, Envify, and Dotenv

April 1, 2019

I found that it’s possible to mix several javascript libraries together to generate production and test bundles for browsers. I was specifically looking for a way to use

  • browserify - bundling multiple javascript files that require each other together.
  • envify - Interpolating environment variables into javascript code
  • dotenv - Setting environment variables from a file
  • nyc - An istanbul CLI for getting instrumenting code and getting test coverage
  • mochify - A pipeline to run mocha tests in a headless browser with browserify

For a production build with a browserify js script, you can use:

const browserify = require('browserify');
require('dotenv').config();

browserify('target.js')
  .transform('envify')
  .bundle()
  .pipe(process.stdout);

which will generate a bundled javascript output with dotenv variables interpolated with envify. For a test build directly as a shell command, you can use:

nyc --require dotenv/config mochify --transform envify

which will run in-browser tests after applying browserify and envify (with dotenv variables).