简体   繁体   English

webpack中如何Polyfill节点核心模块 5

[英]How to Polyfill node core modules in webpack 5

webpack 5 no longer do auto-polyfilling for node core modules. webpack 5 不再对节点核心模块进行自动填充。 How to fix it please?请问如何解决?

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.重大变化:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 This is no longer the case.这已不再是这种情况。 Verify if you need this module and configure a polyfill for it.验证您是否需要此模块并为其配置一个 polyfill。

错误

I was also getting these error's when upgrading from webpack v4 to v5.从 webpack v4 升级到 v5 时,我也遇到了这些错误。 Resolved by making the following changes to webpack.config.js通过对webpack.config.js进行以下更改来解决

added resolve.fallback property添加了 resolve.fallback 属性

removed node property删除节点属性

{
resolve: {
  modules: [...],
  fallback: {
    "fs": false,
    "tls": false,
    "net": false,
    "path": false,
    "zlib": false,
    "http": false,
    "https": false,
    "stream": false,
    "crypto": false,
    "crypto-browserify": require.resolve('crypto-browserify'), //if you want to use this module also don't forget npm i crypto-browserify 
  } 
},
entry: [...],
output: {...},
module: {
  rules: [...]
},
plugins: [...],
optimization: {
  minimizer: [...],
},
// node: {
//   fs: 'empty',
//   net: 'empty',
//   tls: 'empty'
// },
}

upgrade from v4 to v5 => https://webpack.js.org/migrate/5/#clean-up-configuration从 v4 升级到 v5 => https://webpack.js.org/migrate/5/#clean-up-configuration

I think most the answers here would resolve your issue.我认为这里的大多数答案都可以解决您的问题。 However, if you don't need Polyfills for your node development, then I suggest using target: 'node' in your Webpack module configuration.但是,如果您的节点开发不需要 Polyfills,那么我建议在您的 Webpack 模块配置中使用target: 'node' This helped resolve the issue for me.这有助于为我解决问题。

Here is some documentation on the answer: https://webpack.js.org/concepts/targets/以下是有关答案的一些文档: https ://webpack.js.org/concepts/targets/

在此处输入图像描述

Re-add support for Node.js core modules with node-polyfill-webpack-plugin :使用node-polyfill-webpack-plugin重新添加对 Node.js 核心模块的支持:

With the package installed, add the following to your webpack.config.js:安装包后,将以下内容添加到您的 webpack.config.js 中:

const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")

module.exports = {
    // Other rules...
    plugins: [
        new NodePolyfillPlugin()
    ]
}

I faced this issue with create-react-app which by default gave me我用create-react-app遇到了这个问题,默认情况下给了我

"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
"web-vitals": "^2.1.2"

I fixed it by just changing react-scripts to version 4.0.3 .我通过将react-scripts更改为4.0.3版本来修复它。

Looks like you've used the path package which is not included in webpack builds by default.看起来您使用了默认情况下不包含在 webpack 构建中的path包。 For me, extending the webpack config like this helped:对我来说,像这样扩展 webpack 配置有帮助:

{
  // rest of the webpack config
  resolve: {
    // ... rest of the resolve config
    fallback: {
      "path": require.resolve("path-browserify")
    }
  },
}

Also install path-browserify via npm install path-browserify --save-dev or yarn add path-browserify --dev if you're using yarn.如果您使用的是 yarn,还可以通过 npm install path-browserify npm install path-browserify --save-devyarn add path-browserify --dev安装 path-browserify。

As per Web3 documentation :根据Web3 文档

If you are using create-react-app version >=5 you may run into issues building.如果您使用的是 create-react-app 版本 >=5,您可能会遇到构建问题。 This is because NodeJS polyfills are not included in the latest version of create-react-app.这是因为最新版本的 create-react-app 中不包含 NodeJS 的 polyfill。

Solution:解决方案:

Install react-app-rewired and the missing modules安装 react-app-rewired 和缺少的模块

If you are using yarn:如果您使用纱线:

yarn add --dev react-app-rewired process crypto-browserify stream-browserify assert stream-http https-browserify os-browserify url buffer

If you are using npm:如果您使用的是 npm:

npm install --save-dev react-app-rewired crypto-browserify stream-browserify assert stream-http https-browserify os-browserify url buffer process

Create config-overrides.js in the root of your project folder with the content:在项目文件夹的根目录中创建 config-overrides.js,内容如下:

const webpack = require('webpack');

module.exports = function override(config) {
    const fallback = config.resolve.fallback || {};
    Object.assign(fallback, {
        "crypto": require.resolve("crypto-browserify"),
        "stream": require.resolve("stream-browserify"),
        "assert": require.resolve("assert"),
        "http": require.resolve("stream-http"),
        "https": require.resolve("https-browserify"),
        "os": require.resolve("os-browserify"),
        "url": require.resolve("url")
    })
    config.resolve.fallback = fallback;
    config.plugins = (config.plugins || []).concat([
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer']
        })
    ])
    return config;
}

Within package.json change the scripts field for start, build and test.在 package.json 中更改启动、构建和测试的脚本字段。 Instead of react-scripts replace it with react-app-rewired before:而不是 react-scripts 之前用 react-app-rewired 替换它:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
},

after:后:

"scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test",
    "eject": "react-scripts eject"
},

The missing Nodejs polyfills should be included now and your app should be functional with web3.现在应该包含缺少的 Nodejs polyfill,并且您的应用程序应该可以与 web3 一起使用。

If you want to hide the warnings created by the console:如果要隐藏控制台创建的警告:

In config-overrides.js within the override function, add:在覆盖函数内的 config-overrides.js 中,添加:

config.ignoreWarnings = [/Failed to parse source map/];

You need React => v17 React scripts=> v5 webpack => v5你需要 React => v17 React 脚本=> v5 webpack => v5

To Fix The Problem解决问题

1) Install 1) 安装

"fs": "^2.0.0",  // npm i fs
"assert": "^2.0.0",  // npm i assert
"https-browserify": "^1.0.0", // npm i https-browserify
"os": "^0.1.2", // npm i os
"os-browserify": "^0.3.0", // npm i os-browserify
"react-app-rewired": "^2.1.9", //npm i react-app-rewired
"stream-browserify": "^3.0.0", // stream-browserify
"stream-http": "^3.2.0", //stream-http

2) Creating config-overrides.js in root directory Image 2) 在根目录Image创建 config-overrides.js

3) Add configs to config-overrides.js 3) 将配置添加到 config-overrides.js

const webpack = require('webpack');
module.exports = function override(config, env) {
    config.resolve.fallback = {
        url: require.resolve('url'),
        fs: require.resolve('fs'),
        assert: require.resolve('assert'),
        crypto: require.resolve('crypto-browserify'),
        http: require.resolve('stream-http'),
        https: require.resolve('https-browserify'),
        os: require.resolve('os-browserify/browser'),
        buffer: require.resolve('buffer'),
        stream: require.resolve('stream-browserify'),
    };
    config.plugins.push(
        new webpack.ProvidePlugin({
            process: 'process/browser',
            Buffer: ['buffer', 'Buffer'],
        }),
    );

    return config;
}

3) Change packages.json 3)更改packages.json

"scripts": {
  "start": "react-app-rewired start",
  "build": "react-app-rewired build",
  "test": "react-app-rewired test",
  "eject": "react-app-rewired eject"
}

Problem solved :)问题解决了 :)

Create React App just released v5 which now implements Webpack v5. Create React App 刚刚发布了 v5,现在实现了 Webpack v5。 This is going to fully break many libraries which includes web3 as the required Node core library polyfills will be missing.这将完全破坏包括 web3 在内的许多库,因为所需的 Node 核心库 polyfill 将丢失。

To resolve this quickly you can change this in your package.json:要快速解决此问题,您可以在 package.json 中进行更改:

"react-scripts": "^5.0.0"

To this对此

"react-scripts": "4.0.3"

After this:在这之后:

rm -r node_modules

rm package-lock.json

npm install

It happen with me while I reinstall the node modules my webpack current version is 5.38.1 , I have fixed the issue with npm i path-browserify -D after installation you have to update your webpack.config.js resolve{} with fallback: {"fs": false, "path": require.resolve("path-browserify")} while not using "fs": false it show errors ie: Module not found: Error: Can't resolve 'fs' in '/YOUR DIRECTORY ...' so don't forget to add it;当我重新安装节点模块时发生这种情况,我的 webpack 当前版本是5.38.1 ,我已经用npm i path-browserify -D解决了安装后你必须更新你的webpack.config.js resolve{}的问题fallback: {"fs": false, "path": require.resolve("path-browserify")}不使用"fs": false时显示错误,即: Module not found: Error: Can't resolve 'fs' in '/YOUR DIRECTORY ...'所以不要忘记添加它; with other stuff it look like:与其他东西看起来像:

module.exports = {
   ...
   resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],// other stuff
    fallback: {
      "fs": false,
      "path": require.resolve("path-browserify")
    }
  },
};

remove node property if it exist in your webpack.config.js file如果webpack.config.js文件中存在node属性,则删除它

My environment is like this:我的环境是这样的:

  • React => v17反应 => v17
  • React scripts=> v5反应脚本=> v5
  • webpack => v5 webpack => v5

To Fix The Problem follow the below instructions要解决问题,请按照以下说明操作

1- Install below packages 1-安装以下软件包

yarn add fs assert https-browserify os os-browserify stream-browserify stream-http react-app-rewired

2- Create config-coverrides.js in Root dir of your project next to the package.json 2- 在 package.json 旁边的项目的根目录中创建config-coverrides.js

add the below code to it:将以下代码添加到其中:

 const webpack = require('webpack'); module.exports = function override(config, env) { config.resolve.fallback = { url: require.resolve('url'), fs: require.resolve('fs'), assert: require.resolve('assert'), crypto: require.resolve('crypto-browserify'), http: require.resolve('stream-http'), https: require.resolve('https-browserify'), os: require.resolve('os-browserify/browser'), buffer: require.resolve('buffer'), stream: require.resolve('stream-browserify'), }; config.plugins.push( new webpack.ProvidePlugin({ process: 'process/browser', Buffer: ['buffer', 'Buffer'], }), ); return config; }

3- Change the packages.js like the below 3-更改 packages.js 如下

 "scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" },

npm install path-browserify and then try to change webpack configuration to include: npm install path-browserify browserify 然后尝试更改 webpack 配置以包括:

module.exports = {
    ...
    resolve: {
        alias: {
            path: require.resolve("path-browserify")
        }
    }
};

Method 1方法一

  • Open project/node_modules/react-scripts/config/webpack.config.js打开project/node_modules/react-scripts/config/webpack.config.js

  • In fallback add "crypto": require.resolve("crypto-browserify")在后备中添加"crypto": require.resolve("crypto-browserify")

resolve: {
   fallback: {
       "crypto": require.resolve("crypto-browserify")
   }
} 
  • Install npm i crypto-browserify安装npm i crypto-browserify
  • Restart your app.重新启动您的应用程序。

Above method doesn't work if you commit since we aren't node_modules如果您提交,上述方法将不起作用,因为我们不是node_modules

Method 2方法二

  • Install patch-package: yarn add patch-package安装补丁包: yarn add patch-package

  • Install the needed pollyfills.(do an initial build of your application and it will tell you.)安装所需的 pollyfills。(对您的应用程序进行初始构建,它会告诉您。)

  • Modify node_modules/react-scripts/config/webpack.config.js .修改node_modules/react-scripts/config/webpack.config.js Here's an example .这是一个例子 This is taken from Webpack's docs.这取自 Webpack 的文档。

module.exports = {
  //...
  resolve: {
    fallback: {
      assert: require.resolve('assert'),
      buffer: require.resolve('buffer'),
      console: require.resolve('console-browserify'),
      constants: require.resolve('constants-browserify'),
      crypto: require.resolve('crypto-browserify'),
      domain: require.resolve('domain-browser'),
      events: require.resolve('events'),
      http: require.resolve('stream-http'),
      https: require.resolve('https-browserify'),
      os: require.resolve('os-browserify/browser'),
      path: require.resolve('path-browserify'),
      punycode: require.resolve('punycode'),
      process: require.resolve('process/browser'),
      querystring: require.resolve('querystring-es3'),
      stream: require.resolve('stream-browserify'),
      string_decoder: require.resolve('string_decoder'),
      sys: require.resolve('util'),
      timers: require.resolve('timers-browserify'),
      tty: require.resolve('tty-browserify'),
      url: require.resolve('url'),
      util: require.resolve('util'),
      vm: require.resolve('vm-browserify'),
      zlib: require.resolve('browserify-zlib'),
    },
  },
};
  • Don't add all of them, add only the ones you need.不要全部添加,只添加您需要的。

Make sure you install the packages first before modifying the webpack config.确保在修改 webpack 配置之前先安装包。

  • Run yarn patch-package react-scripts .运行yarn patch-package react-scripts This will generate a patch (this should be committed in your repo going forward).这将生成一个补丁(这应该在您的回购中提交)。

  • Add a postinstall script to package.json: "postinstall": "yarn patch-package" .将安装后脚本添加到 package.json: "postinstall": "yarn patch-package" Now, anytime, someone installs npm deps on this project, will get the patch you created in step 3 applied automatically.现在,任何时候,有人在这个项目上安装 npm deps,都会自动应用你在步骤 3 中创建的补丁。

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "postinstall": "yarn patch-package"
  },

My solution for VUE我的 VUE 解决方案

const { defineConfig } = require('@vue/cli-service')
const webpack = require('webpack');
module.exports = defineConfig({
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
      }),
      new webpack.ProvidePlugin({
          process: 'process/browser',
      })
    ],
    resolve: {
      fallback: {
        "os": require.resolve("os-browserify/browser"),
        "url": require.resolve("url/"),
        "crypto": require.resolve("crypto-browserify"),
        "https": require.resolve("https-browserify"),
        "http": require.resolve("stream-http"),
        "assert": require.resolve("assert/"),
        "stream": require.resolve("stream-browserify"),
        "buffer": require.resolve("buffer")
      }
    }
  },

  transpileDependencies: [
    'vuetify'
  ]

})

I'm using create-react-app with craco , and I encountered the following errors when upgrading to webpack 5:我将create-react-appcraco一起使用,升级到 webpack 5 时遇到以下错误:

'buffer' '缓冲'


Module not found: Error: Can't resolve 'buffer' in '/Users/geek.neo/propolis-portal/node_modules/safe-buffer'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    - install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "buffer": false }

This was resolved simply by installing the buffer package with npm install -D buffer .只需使用npm install -D buffer安装缓冲区包即可解决此问题。

'fs' 'fs'


Module not found: Error: Can't resolve 'fs' in '/Users/geek.neo/propolis-portal/node_modules/line-navigator'

This was resolved by setting a webpack fallback in the craco configuration craco.config.js :这已通过在 craco 配置craco.config.js中设置 webpack 后备来解决:

module.exports = {
    style: {
        postcssOptions: {
            plugins: [
            require('tailwindcss'),
            require('autoprefixer'),
            ],
        },
    },
    webpack: {
        configure: (webpackConfig, { env, paths }) => {
            // eslint-disable-next-line no-param-reassign
            webpackConfig.resolve.fallback = {
                fs: false,
            };
            return webpackConfig;
        },
    },
}

you want to used in你想用在

const nodeExternals = require('webpack-node-externals');

add in webpack.config.js添加 webpack.config.js

target: "node",
devtool: "source-map",
externals: [nodeExternals()],
npm install assert --save

npm install buffer --save

For anyone facing similar issue, just install the missing modules.对于任何面临类似问题的人,只需安装缺少的模块。 These modules are reported as missing because they are part of node.js, but are available separately also via the npm.这些模块被报告为缺失,因为它们是 node.js 的一部分,但也可以通过 npm 单独使用。

This is happening with the new vue-cli upgrade (v5).新的 vue-cli 升级 (v5) 正在发生这种情况。 In order to fix it (the empty modules way), you have to change vue.config.js this way:为了修复它(空模块方式),您必须以这种方式更改vue.config.js

configureWebpack: (config) => {
  config.resolve.fallback = {
    ...config.resolve.fallback,
    // Include here the "empty" modules
    url: false,
    util: false,
    querystring: false,
    https: false,
  };
}

Fix this problem with this i try this and it's works用这个解决这个问题我试试这个,它的工作原理

https://github.com/facebook/create-react-app/issues/11756#issuecomment-1001162736 https://github.com/facebook/create-react-app/issues/11756#issuecomment-1001162736

My app threw the same error yesterday.我的应用昨天抛出了同样的错误。 I spent hours reading questions/answers here on SO and trying some.我花了几个小时在 SO 上阅读问题/答案并尝试了一些。 What has worked for me is this:对我有用的是:

https://github.com/ChainSafe/web3.js#troubleshooting-and-known-issues https://github.com/ChainSafe/web3.js#troubleshooting-and-known-issues

If it's Twilio you use:如果您使用的是 Twilio:

The Twilio module is not intended for use in client-side JavaScript, which is why it fails for your Angular application. Twilio 模块不适用于客户端 JavaScript,这就是它对 Angular 应用程序失败的原因。 Twilio uses your Account SID and your Auth Token, the use in the client side represents a risk; Twilio 使用您的 Account SID 和您的 Auth Token,在客户端使用存在风险; So the best is to use it on the server side and to use the API.所以最好是在服务器端使用它并使用API​​。

With create-react-app v17 and scripts 5.0.0 you need to add a fallback to your webpack.config.js and install 'process'.使用 create-react-app v17 和脚本 5.0.0,您需要向 webpack.config.js 添加回退并安装“进程”。

   `module.exports = function (webpackEnv) {
      .........
      return {
       .........
      resolve: {
      ..........
      fallback: { "process": require.resolve("process/browser") },`

Secret's answer very nearly worked for me (I don't have enough reputation yet to comment on there, sorry!)秘密的回答几乎对我有用(我还没有足够的声誉来评论那里,对不起!)

After having followed the steps in their answer it then told me that because of the difference in required versions of eslint, I should add SKIP_PREFLIGHT_CHECK=true to a .env file in the project, so I just added it to my existing one.在按照他们回答中的步骤进行操作后,它告诉我,由于所需版本的 eslint 不同,我应该将SKIP_PREFLIGHT_CHECK=true添加到项目中的 .env 文件中,所以我只是将其添加到我现有的文件中。

It would then successfully build (finally!) But then I noticed, in Chrome at least, that I couldn't click on anything or even select any text.然后它会成功构建(终于!)但后来我注意到,至少在 Chrome 中,我无法单击任何内容,甚至无法选择任何文本。 Turns out that there's still an Iframe over the top of everything that can be removed in Inspector.事实证明,在 Inspector 中可以删除的所有内容的顶部仍然有一个 iframe。 - This applies when running a dev build, npm run start , I am not sure if it does it on a production build. - 这适用于运行开发版本npm run start ,我不确定它是否在生产版本中执行。

I must say this sudden change IMO really hasn't been very well thought through!我必须说,IMO 的这种突然变化真的没有经过深思熟虑!

my problem was that I was trying to use JSON.parse, then when I started writing , auto complete showed me json(note small letters) though I renamed it to JSON.我的问题是我试图使用 JSON.parse,然后当我开始写作时,自动完成向我显示了 json(注意小写字母),尽管我将它重命名为 JSON。 when I pressed enter it automatically imported (import { json } from "express/lib/response";) it was the cause and I didn't notice it at all , later in development my app was crushing and it took me about six hours to note, since the app is quite big.当我按 Enter 时,它会自动导入(从“express/lib/response”中导入 { json };)这是原因,我根本没有注意到,后来在开发中我的应用程序崩溃了,我花了大约六个小时需要注意的是,因为该应用程序很大。

so if none of the above solutions work just see if you didn't import something .因此,如果上述解决方案都不起作用,请查看您是否没有导入某些内容。

I found a solution that worked for me:我找到了一个适合我的解决方案:

  1. npm uninstall webpack
  2. delete the "package-lock.json" file删除“package-lock.json”文件
  3. npm install webpack@4.44.2 --force
  4. include in the "package.json" file the following in your "scripts":在“脚本”中的“package.json”文件中包含以下内容:
     "scripts": { "start": "SET NODE_OPTIONS=--openssl-legacy-provider && react-scripts start", "build": "SET NODE_OPTIONS=--openssl-legacy-provider && react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }

I got this error while imported Router from express import { Router } from 'express';从 express import { Router } from 'express' 导入路由器时出现此错误;

Resolved after correction import { Router } from '@angular/router';修正 import { Router } from '@angular/router' 后解决;

Faced the same issue, here's solution:遇到同样的问题,解决方法如下:

  1. remove "package-lock.json" from the project folder and 'npm uninstall webpack'从项目文件夹中删除“package-lock.json”和“npm uninstall webpack”
  2. downgrade "react-script" from '5.something' to '4.0.3'将“react-script”从“5.something”降级为“4.0.3”
  3. make sure "package-lock.json" is deleted/removed确保“package-lock.json”被删除/移除
  4. Install webpack using 'npm install webpack@4.44.2'使用 'npm install webpack@4.44.2' 安装 webpack
  5. Finally, run 'npm install' using the terminal最后,使用终端运行“npm install”

I tried this https://stackoverflow.com/a/71803628/15658978 solution and it solved the problem form me.我尝试了这个https://stackoverflow.com/a/71803628/15658978解决方案,它解决了我的问题。

simply run the following 2 commands只需运行以下 2 个命令

npm uninstall react-scripts
npm install react-scripts@4.0.3
Compiled with problems:X

ERROR in ./node_modules/body-parser/lib/read.js 24:11-26

Module not found: Error: Can't resolve 'zlib' in 'E:\My Documents\work\web\mbamasala\node_modules\body-parser\lib'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }'
    - install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "zlib": false }


ERROR in ./node_modules/body-parser/lib/types/urlencoded.js 217:12-34

Module not found: Error: Can't resolve 'querystring' in 'E:\My Documents\work\web\mbamasala\node_modules\body-parser\lib\types'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "querystring": require.resolve("querystring-es3") }'
    - install 'querystring-es3'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "querystring": false }


ERROR in ./node_modules/destroy/index.js 15:17-41

Module not found: Error: Can't resolve 'fs' in 'E:\My Documents\work\web\mbamasala\node_modules\destroy'


ERROR in ./node_modules/destroy/index.js 17:13-30

Module not found: Error: Can't resolve 'stream' in 'E:\My Documents\work\web\mbamasala\node_modules\destroy'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
    - install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "stream": false }


ERROR in ./node_modules/destroy/index.js 19:11-26

Module not found: Error: Can't resolve 'zlib' in 'E:\My Documents\work\web\mbamasala\node_modules\destroy'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "zlib": require.resolve("browserify-zlib") }'
    - install 'browserify-zlib'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "zlib": false }


ERROR in ./node_modules/mime-types/index.js 15:14-37

Module not found: Error: Can't resolve 'path' in 'E:\My Documents\work\web\mbamasala\node_modules\mime-types'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }


ERROR in ./node_modules/safer-buffer/safer.js 4:13-30

Module not found: Error: Can't resolve 'buffer' in 'E:\My Documents\work\web\mbamasala\node_modules\safer-buffer'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    - install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "buffer": false }


ERROR in ./node_modules/string_decoder/node_modules/safe-buffer/index.js 4:13-30

Module not found: Error: Can't resolve 'buffer' in 'E:\My Documents\work\web\mbamasala\node_modules\string_decoder\node_modules\safe-buffer'

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.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
    - install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "buffer": false }

I tried many solution but didn't resolve.我尝试了很多解决方案,但没有解决。 After Searching manually I got a single line of code which was inserted automatically in my vs code which was import {json} from 'body-parser' I removed and saved my time.手动搜索后,我得到了一行代码,它自动插入到我的 vs 代码中,它是import {json} from 'body-parser'我删除并节省了我的时间。 I hope it may help someone.我希望它可以帮助某人。 Thanks.谢谢。

For me (using React => v17; React scripts=> v5; webpack => v5), a combination of solutions suggested by @Letton and @Richie Bendall worked along with another addition, ie to the package.json, the following packages had to be added and installed. For me (using React => v17; React scripts=> v5; webpack => v5), a combination of solutions suggested by @Letton and @Richie Bendall worked along with another addition, ie to the package.json, the following packages had要添加和安装。 Please also note that fs version "^2.0.0" throws an error, so changed to "0.0.1-security".另请注意,fs 版本“^2.0.0”会引发错误,因此更改为“0.0.1-security”。

"assert": "^2.0.0", 
"https-browserify": "^1.0.0", 
"os": "^0.1.2",
"os-browserify": "^0.3.0",
"react-app-rewired": "^2.1.9",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"fs":"^0.0.1-security",
"crypto-browserify": "3.12.0",
"buffer":"^6.0.3",
"node-polyfill-webpack-plugin": "^2.0.1"

After installation of these, executing the remaining steps, ie adding config-overrides.js to the root directory and changing scripts content in package.json from react-scripts to react-app-rewired fixed the problem.安装这些之后,执行剩余的步骤,即将 config-overrides.js 添加到根目录并将 package.json 中的脚本内容从 react-scripts 更改为 react-app-rewired 修复了问题。

Make sure you are not importing the following from express or even json or something else确保您没有从 express 或什json或其他东西导入以下内容

import { response } from 'express' // response in my case

For anyone who is using React (happened to me in react).对于任何使用 React 的人(在 React 中发生在我身上)。 I have even read with NextJs this works but haven't tested it.我什至用 NextJs 读过这个作品,但还没有测试过。

Man I did everything like config-overrides (Vitto's answer) and node modules webpack config file updates and also changed react-scripts version 5 to 4 but nothing worked.伙计,我做了一切,比如配置覆盖(Vitto 的回答)和节点模块 webpack 配置文件更新,并将 react-scripts 版本 5 更改为 4,但没有任何效果。 But as soon as I removed response .但是一旦我删除了response It worked.有效。

That one line only made me go into 7 hours of debugging mode.那一行只让我 go 进入了 7 个小时的调试模式。

But always enjoy it:)但总是喜欢它:)

ERROR : [webpack < 5 used to include polyfills for node.js core modules by default.错误:[webpack < 5 用于默认包含 node.js 核心模块的 polyfill。

This is no longer the case.这已不再是这种情况。 Verify if you need this module and configure a polyfill for it.验证你是否需要这个模块并为它配置一个 polyfill。

Answer: node_modules > react-scripts > config > webpack.config.js答案:node_modules > react-scripts > config > webpack.config.js

Add in webpack.config.js file:添加 webpack.config.js 文件:

resolve: {
      fallback: { 
        "http": require.resolve("stream-http") ,
         "path": require.resolve("path-browserify") 
        },
}

I like to keep things simple, no need to run npm run-script eject until here, so I downgrade back to react-scripts@4.0.3 instead of 5.0.0 Until now, this is still not fixed in Webpack我喜欢保持简单,直到这里不需要运行 npm run-script injection,所以我降级回 react-scripts@4.0.3 而不是 5.0.0 直到现在,这在 Webpack 中仍然没有修复

Sadly, because that includes a bunch of retired packages.可悲的是,因为这包括一堆退休的包裹。

for me, I just removed an unused import called:对我来说,我刚刚删除了一个未使用的导入,名为:

import res from "express/lib/response"

and it fixed it!它修复了它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM