7

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

Liam
  • 27,717
  • 28
  • 128
  • 190
Abdus Shaikh
  • 97
  • 1
  • 2

3 Answers3

4

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",
      }),
    ],
    
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • Still having an issue with the packages. It is complaining now about Module not found: Error: Can't resolve 'url' in '/node_modules/https-browserify'. Going to see if there is any guide for breaking changes in webpack 5 and go from there. – 7FigureSwagger Jan 10 '22 at 19:25
  • 1
    @7FigureSwagger So you have to install url, and add it to “fallback” like others. There are too many polyfills. Each packade needs different ones – Yilmaz Jan 10 '22 at 19:44
  • 1
    @7FigureSwagger you can also use https://www.npmjs.com/package/node-polyfill-webpack-plugin instead of installing packages manually. I updated the answer – Yilmaz Jan 10 '22 at 20:08
  • Thanks for all the help, that package does the trick! Although I still am having issues, looks like because i ejected i need to move some files around? My source is in 'src' and i have the 'public' folder after 'ejecting', I am reading that 'src' files need to be inside 'public'? I have yet to have to manage this part myself, so I don't know where to go from here. – 7FigureSwagger Jan 10 '22 at 22:26
  • 1
    @7FigureSwagger if you are using create-react-app, check this out: https://stackoverflow.com/questions/70472965/web3-issue-react-application-not-compiling/70512623#70512623 – Yilmaz Jan 10 '22 at 22:28
  • 1
    Cool, yeah I saw that actually, going to try your boilerplate out now. Any tips? I tried looking at your configs/project structure vs mine, I was able to replicate most, but clearly you intentionally put this together, did you use nx? I have heard of but not used nx. Was a recommended option elsewhere. Got your boilerplate running now, but if i can suggest making webpack 5.56.2 a dependancy? I had to upgrade it as I didn't have legacy support for MD4 (I'm on MacOS). After upgrading webpack, all it well and your test site runs, now to migrate my front-end and try hooking up web3 again lol... – 7FigureSwagger Jan 11 '22 at 01:11
  • 1
    @7FigureSwagger are you talking about this: https://nx.dev/ Never heard before. Looks promising – Yilmaz Jan 11 '22 at 01:18
  • 1
    Yeah! I am still a bit new to be understanding all the intricacies of a 'build', and what and why i need this or that in my build. BUT, I got the jist, and that was to help build the stack that I need without extras? As far as I can tell... – 7FigureSwagger Jan 11 '22 at 01:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240942/discussion-between-7figureswagger-and-yilmaz). – 7FigureSwagger Jan 11 '22 at 01:38
1

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.

-1

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;
  },
};
  • Whats wrong with just using the `NodePolyfillPlugin` plugin? – Liam Apr 04 '23 at 13:05
  • Also, this is essentially the [same as this answer](https://stackoverflow.com/a/70560529/542251) – Liam Apr 04 '23 at 13:05
  • yes NodePolyfillPlugin can be useful but it depends on the situation, we cannot ignore increasing bundle size without many purposes, also not ignore the performance impact and there still can be compatibility issues – Adnan Hussain Apr 04 '23 at 20:25