webpack破天荒

2022-02-21 00:00:00 reactjs javascript webpack npm

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

找不到模块:错误:无法解析‘/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time’

中的‘BUFFER

突破性变化:webpack<;5默认包含node.js核心模块的PolyFill。 现在已经不是这样了。请验证您是否需要此模块,并为其配置多边形填充。

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


解决方案

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

  • 由于多边形填充太多,您可以使用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",
      }),
    ],
    

相关文章