【问题标题】:Webpack breaking changeWebpack 重大变化
【发布时间】:2022-01-02 20:10:47
【问题描述】:

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

找不到模块:错误:无法解析“/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time”中的“缓冲区”

重大变化:webpack

它为几个不同的模块提供相同的信息。我已经尝试 npm 安装这些模块,但错误仍然存​​在

【问题讨论】:

  • 既然你显然确实需要那个模块,你有没有尝试配置一个polyfill?还是坚持使用 Webpack 4?
  • 我对 Web 开发很陌生。如何恢复到 Webpack 4?
  • 你是如何安装 Webpack 5 的?对 v4 执行相同操作。研究如何使用 NPM。
  • edit提供minimal reproducible example的问题。
  • 这部分与您实际询问的内容相关吗? MRE 的重点是将其缩减为仅重现问题所需的内容。

标签: javascript reactjs npm webpack


【解决方案1】:

这是我的 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

  • 由于polyfills太多,不用手动全部安装,可以使用node-polyfill-webpack-plugin包。而不是fallback 属性

     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",
      }),
    ],
    

【讨论】:

  • @7FigureSwagger 所以你必须安装 url,并像其他人一样将其添加到“后备”中。 polyfill 太多了。每个包装需要不同的
  • @7FigureSwagger 你也可以使用npmjs.com/package/node-polyfill-webpack-plugin 而不是手动安装包。我更新了答案
  • @7FigureSwagger 如果您使用的是 create-react-app,请查看:stackoverflow.com/questions/70472965/…
  • 酷,是的,我确实看到了,现在要试试你的样板。有小费吗?我尝试查看您的配置/项目结构与我的,我能够复制大部分,但显然您故意将它们放在一起,您使用 nx 吗?我听说过但没用过nx。是其他地方的推荐选择。现在你的样板已经运行了,但如果我可以建议让 webpack 5.56.2 成为一个依赖项?我不得不升级它,因为我没有对 MD4 的旧版支持(我在 MacOS 上)。升级 webpack 后,一切顺利,您的测试站点运行,现在迁移我的前端并尝试再次连接 web3 大声笑...
  • @7FigureSwagger 你在说这个吗:nx.dev 以前从未听说过。看起来很有希望
【解决方案2】:

您似乎正在使用前端反应应用程序,并且某些依赖项在内部使用 buffer 模块,该模块仅在 webpack 下的 target: node 中可用。所以你需要为它添加一个 polyfill。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 2021-09-13
    • 2011-09-27
    • 2021-08-01
    • 2011-02-02
    相关资源
    最近更新 更多