javascriptbuildbrunch

Brunch builds but "Cannot find module 'buffer'" on open


We are attempting to transition our front end build to use Brunch. Here's the Brunch configuration I have so far:

module.exports = {

  files: {
    javascripts: {
      joinTo: {
        'vendor.js': /^(?!source)/,
        'app.js': /^source/
      },
      entryPoints: {
        'source/scripts/app.jsx': 'app.js'
      }
    },
    stylesheets: {joinTo: 'core.css'},
  },

  paths: {
    watched: ['source']
  },

  modules: {
    autoRequire: {
      'app.js': ['source/scripts/app']
    }
  },

  plugins: {
    babel: {presets: ['latest', 'react']},
    postcss: {processors: [require('autoprefixer')]},
    assetsmanager: {
      copyTo: {
        'assets': ['source/resources/*']
      }
    },
    static: {
      processors: [
        require('html-brunch-static')({
          processors: [
            require('pug-brunch-static')({
              fileMatch: 'source/views/home.pug',
              fileTransform: (filename) => {
                filename = filename.replace(/\.pug$/, '.html');
                filename = filename.replace('views/', '');
                return filename;
              }
            })
          ]
        })
      ]
    }

  }

};

I added the modules.autoRequire section to the Brunch configuration and then the following error started happening. Without modules.autoRequire I have no console error but also my web app does not start. Running brunch build results in no errors, but when I open the built website, I get the error

Uncaught Error: Cannot find module 'buffer' from 'lodash/lodash.js'

The first line in the stacktrace points me to this function in vendor.js

var require = function(name, loaderPath) {
  if (loaderPath == null) loaderPath = '/';
  var path = expandAlias(name);

  if (has.call(cache, path)) return cache[path].exports;
  if (has.call(modules, path)) return initModule(path, modules[path]);

  throw new Error("Cannot find module '" + name + "' from '" + loaderPath + "'");
};

I'm not sure how to proceed to get my build working. This issue seems like it may be relevant.

How can I overcome this error? (Please feel free to ask for additional information. I'm not sure what all would be helpful.)


Solution

  • Brunch takes the steps of the build pipeline from the (brunch specific) npm packages defined in the package.json. Therefore makes sure to include all the needed packages (or to remove unnecessary ones).

    See the docs for more information about how to use plugins.