Search code examples
javascriptnode.jsbrowserify

Browserify api: how to pass advanced option to script


I am writing a small script using the Browserify API and following the readme here.

All works ok except when I need to pass a flag listed under the advanced options, for which there seems to be no coverage in the API. What I would like to pass is the --node flag.

var b = browserify({
entries: ['src/index.js'],
  cache: {},
  ignoreWatch: ['**/node_modules/**', './dist/'],
  packageCache: {},
  plugin: [watchify, babelify],
  debug: true,
  node: true // => this options does not actually exist, so it does nothing
});

b.on('update', bundle);

function bundle() {
  b.bundle()
    .pipe(fs.createWriteStream('bundle.js'));
}

In the command line this would translate into watchify src/index.js --node -o bundle.js (and it works).

I think that the line in the docs that says:

All other options are forwarded along to module-deps and browser-pack directly.

may contain some help but it's not clear to me how. Any pointers on this?


Solution

  • After looking at the source code of Browserify I came up with the answer to my own question.

    I ran the watchify src/index.js --node -o bundle.js command and logged the options passed to the Browserify.prototype._createPipeline function.

    In my case the code would become:

    var b = browserify({
    entries: ['src/index.js'],
      cache: {},
      ignoreWatch: ['**/node_modules/**', './dist/'],
      packageCache: {},
      plugin: [watchify, babelify],
      debug: true,
      fullPaths: false,
      builtins: false,
      commondir: false,
      bundleExternal: true,
      basedir: undefined,
      browserField: false,
      detectGlobals: true,
      insertGlobals: false,
      insertGlobalVars:
       { process: undefined,
         global: undefined,
         'Buffer.isBuffer': undefined,
         Buffer: undefined },
      ignoreMissing: false,
      standalone: undefined
    });
    

    I ended up removing some of these options from my script because most are just default values, but I'll leave them in the answer for future reference, hopefully somebody else will benefit from it anyway.