我该如何解决“BREAKING CHANGE:webpack <5默认情况下包含Node.js核心模块的polyfill”的错误?

7

我正在尝试构建一个React应用程序,但每次运行npm start时,都会收到以下消息:

模块未找到:错误:无法解析“buffer”(位于“/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time”中)

BREAKING CHANGE: webpack < 5默认情况下包含node.js核心模块的polyfills。 现在不再是这种情况。请验证是否需要此模块并为其配置polyfill。

对于一些不同的模块,它会给出相同的消息。我已经尝试了npm安装这些模块,但错误仍然存在。


我对Web开发还比较新,如何回退到Webpack 4? - Abdus Shaikh
1
你是怎么安装Webpack 5的?用同样的方法安装v4。研究如何使用NPM。 - jonrsharpe
请编辑问题以提供更小的可重现示例(MRE)。 - jonrsharpe
该程序使用一个需要密钥的API,我不确定它如何工作。 - Abdus Shaikh
1
这部分与你实际询问的内容相关吗?MRE 的重点是将其缩减为仅需要重新创建问题的部分。 - jonrsharpe
显示剩余2条评论
3个回答

5

这是我的Webpack配置,它可以正常工作。您应该安装在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" }],
    // }),
    
  ],
};

你可以获取这个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",
      }),
    ],
    

1
@7FigureSwagger 所以你需要安装URL,并像其他包一样将其添加到“fallback”中。有太多的polyfill。每个包都需要不同的polyfill。 - Yilmaz
1
@7FigureSwagger 您也可以使用 https://www.npmjs.com/package/node-polyfill-webpack-plugin 来代替手动安装包。我已经更新了答案。 - Yilmaz
1
如果您正在使用create-react-app,请查看此链接:https://dev59.com/fW0NtIcB2Jgan1znPdxx#70512623 @7FigureSwagger - Yilmaz
1
酷,是的,我确实看到了,现在要尝试使用你的样板文件。有什么建议吗?我试着查看你的配置/项目结构和我的对比,我能够复制大部分,但显然你是有意将它们组合在一起的,你用了nx吗?我听说过但没有使用过nx。在其他地方推荐过。现在我已经让你的样板文件运行起来了,但如果我可以建议的话,可以将webpack 5.56.2作为依赖项吗?我不支持MD4(我在MacOS上),所以必须升级webpack。升级webpack后,一切都很好,你的测试站点也可以运行,现在要迁移我的前端并再次尝试连接web3了lol... - 7FigureSwagger
1
@7FigureSwagger 你是在说这个吗:https://nx.dev/ 以前没听说过。看起来很有前途。 - Yilmaz
显示剩余4条评论

1

看起来您正在使用前端React应用程序,某些依赖项在webpack下仅适用于target:nodebuffer模块。因此,您需要为此添加一个polyfill。

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

您可以在webpack这里查看文档:https://webpack.js.org/configuration/resolve/#resolvefallback

从Webpack 5开始,webpack不再为基于浏览器的应用程序进行polyfill。


-1
在我的React应用程序中没有webpack.config.js文件,这里是我如何解决问题的方法:
1- 首先,安装buffer包: npm install buffer 2- 在你的项目根目录下创建一个名为的文件。

react-app-rewired.config.js

3- 将以下代码添加到文件中:

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;
  },
};

仅使用NodePolyfillPlugin插件有什么问题吗? - Liam
同样地,这基本上与此答案相同。 - Liam
是的,NodePolyfillPlugin可能很有用,但这取决于具体情况。我们不能忽视包大小的增加,也不能忽略性能影响,还可能存在兼容性问题。 - Adnan Hussain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接