我正在尝试构建一个 React 应用程序,但每次运行 npm start 时,都会收到此消息
找不到模块:错误:无法解析“/Users/abdus/Documents/GitHub/keywords-tracker/node_modules/buffer-equal-constant-time”中的“缓冲区”
重大变更: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
由于polyfill太多,您可以使用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",
}),
],
您似乎正在使用前端 React 应用程序,并且某些依赖项在内部使用
buffer
模块,该模块仅在 webpack 下的 target: node
中可用。所以你需要为其添加一个polyfill。
module.exports = {
resolve: {
fallback: {
buffer: require.resolve('buffer'),
}
},
}
您可以在 webpack 上查看文档:https://webpack.js.org/configuration/resolve/#resolvefallback
从 Webpack 5 开始,webpack 不再对基于浏览器的应用程序进行 Polyfill。
在我的react-app 中没有任何文件名 webpack.config.js 所以这是我解决问题的方法:
1- 首先,安装缓冲包: npm 安装缓冲区
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;
},
};