How can I fix the "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default" error?

10.9k views Asked by At

I am trying to build a react app but each time I run npm start, I am greeted with this message

Module not found: Error: Can't resolve 'buffer' in '/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

It gives the same message for a few different modules. I have tried npm installing these modules but the error persists

3

There are 3 answers

9
Yilmaz On

this is my webpack set up that works. you should install all the packages that listed in fallback:

// const path = require("path");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");

module.exports = {
  mode: "development",
  target: "web",
  entry: ["regenerator-runtime/runtime", "./src/index.js"],
  output: {
    filename: "bundle.js",
    path: path.join(__dirname, "dist"),
    publicPath: "/",
  },
  resolve: {
    extensions: [".js", ".css"],
    alias: {
      // add as many aliases as you like!
      components: path.resolve(__dirname, "src/components"),
    },
    fallback: {
      // path: require.resolve("path-browserify"),
      fs: false,
      assert: require.resolve("assert/"),
      os: require.resolve("os-browserify/browser"),
      constants: require.resolve("constants-browserify"),
      stream: require.resolve("stream-browserify"),
      crypto: require.resolve("crypto-browserify"),
      http: require.resolve("stream-http"),
      https: require.resolve("https-browserify"),
    },
  },
  // devtool: "eval-cheap-source-map",
  devtool: "eval",
  module: {
    rules: [
      { test: /\.(js|jsx)/, loader: "babel-loader", exclude: /node_modules/ },
      { test: /\.css$/, use: ["style-loader", "css-loader"] },
      //   {
      //     test: /\.m?js/,
      //     resolve: {
      //         fullySpecified: false
      //     }
      // },
      {
        test: /\.(woff(2)?|ttf|eot|jpg|jpeg|png|gif)(\?v=\d+\.\d+\.\d+)?$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[name].[contenthash].[ext]",
              outputPath: "fonts/",
            },
          },
        ],
      },
      {
        test: /\.svg$/,
        use: [
          {
            loader: "svg-url-loader",
            options: {
              limit: 10000,
            },
          },
        ],
      },
      {
        test: /\.json5$/i,
        loader: "json5-loader",
        type: "javascript/auto",
        options: {
          esModule: true,
        },
      },
    ],
  },
  devServer: {
    contentBase: path.join(__dirname, "build"),
    historyApiFallback: true,
    overlay: true,
  },

  plugins: [
    new HtmlWebpackPlugin({
      title: "NFT",
      template: "src/index.html",
    }),
    // new CopyWebpackPlugin({
    //   patterns: [{ from: "assets", to: "assets" }],
    // }),
    
  ],
};

you can get this webpack5-Boilerplate

  • Since there are too many polyfills, instead of manually installing all, you can use node-polyfill-webpack-plugin package. instead of fallback property

     const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
    
    plugins: [
      new HtmlWebpackPlugin({
        title: "esBUild",
        template: "src/index.html",
      }),
      // instead of fallback
      new NodePolyfillPlugin(),
    
      // new webpack.ProvidePlugin({
      // process: "process/browser",
      // Buffer: ["buffer", "Buffer"],
      // React: "react",
      }),
    ],
    
0
Aditya Gururaja Rao Karnam On

It seems like you are using a front-end react app and some dependency is internally using the buffer module which is only available in target: node under webpack. So you will need to add a polyfill for the same.

module.exports = {
   resolve: {
       fallback: {
           buffer: require.resolve('buffer'),
       }
   },
}

You can check the docs here at webpack: https://webpack.js.org/configuration/resolve/#resolvefallback

From Webpack 5 onwards, webpack doesn't polyfill for browser-based applications.

3
Adnan Hussain On

In my react-app there isnt any file name webpack.config.js so here is how I resolved my problem:

1- First, install the buffer package: npm install buffer

2- Create a file named at the root of your project.

react-app-rewired.config.js

3- Add the following code to the file:

const webpack = require('webpack');
module.exports = {
  webpack: (config, { isServer }) => {
    // Add fallback for 'buffer' module
    if (!isServer) {
      config.resolve.fallback = {
        buffer: require.resolve('buffer'),
      };
    }

    return config;
  },
};