browser-pack-flat

raw JSON →
3.5.0 verified Mon Apr 27 auth: no javascript maintenance

Browserify plugin that bundles modules into a single scope, similar to Rollup, eliminating the require runtime wrapper. Current stable version is 3.5.0. Maintained sporadically; no major release since 2020. Differentiator: drastically reduces bundle size and execution overhead by rewriting require() calls to variable references and flattening module scopes. Unlike default browser-pack, it removes the per-module function wrapper and runtime, at the cost of potential execution order differences and incompatibility with certain patterns (e.g., wrapped require, factor-bundle). Supports an ecmaVersion option and IIFE removal via iife:false.

error Error: Cannot find module 'browser-pack-flat/plugin'
cause Old v3.0.x path; main entry acts as plugin since v3.1.0.
fix
Use require('browser-pack-flat') instead of '/plugin'.
error Unexpected token: punc ())
cause Input is not browser-unpack JSON but raw JavaScript bundle.
fix
Pipe through browser-unpack first: browserify app.js | browser-unpack | browser-pack-flat
error TypeError: browserify(...).plugin is not a function
cause Forgetting to call .plugin() with the function; passing string instead.
fix
Use .plugin(packFlat) or .plugin(require('browser-pack-flat'))
gotcha Modules are executed fully one after another, not inline. This can change execution order vs Node.js and default browserify.
fix Ensure modules don't rely on interleaved execution; test thoroughly.
gotcha Does not work with factor-bundle; output splitting is incompatible.
fix Use a different bundling strategy if you need code splitting.
gotcha Modules that wrap require() (e.g., with custom caching or instrumentation) will break.
fix Avoid wrapping require; use inline requires only.
breaking In v3.4.0, conditional and lazy require() calls are no longer inlined; they remain as function calls.
fix If you need inlining of dynamic requires, pin to earlier version.
npm install browser-pack-flat
yarn add browser-pack-flat
pnpm add browser-pack-flat

Demonstrates using browser-pack-flat as a browserify plugin with IIFE removal, piping output to a file.

const browserify = require('browserify');
const packFlat = require('browser-pack-flat');
const fs = require('fs');

const b = browserify({ entries: './app.js' });
b.plugin(packFlat, { iife: false });
b.bundle()
  .pipe(fs.createWriteStream('bundle.js'))
  .on('finish', () => console.log('Bundled!'));