gpt4 book ai didi

javascript - Webpack 重大变化

转载 作者:行者123 更新时间:2023-12-05 00:32:28 25 4
gpt4 key购买 nike

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

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.


它为几个不同的模块提供了相同的信息。我已尝试 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
  • 由于polyfills太多,不用手动全部安装,可以使用node-polyfill-webpack-plugin包裹。而不是 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",
    }),
    ],
  • 关于javascript - Webpack 重大变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70559396/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com