Vue-解决BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.

这篇具有很好参考价值的文章主要介绍了Vue-解决BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

报错显示

跑个vue的项目,突然出现以下报错

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: { "console": require.resolve("console-browserify") }'
        - install 'console-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "console": false }
 @ ./src/components/HelloWorld.vue?vue&type=script&lang=js 1:0-202 1:0-202 1:203-394 1:203-394
 @ ./src/components/HelloWorld.vue 2:0-61 3:0-56 3:0-56 6:49-55
 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/App.vue?vue&type=script&lang=js 1:0-53 5:4-14
 @ ./src/App.vue?vue&type=script&lang=js 1:0-189 1:0-189 1:190-368 1:190-368 @ ./src/App.vue 2:0-54 3:0-49 3:0-49 8:49-55
 @ ./src/main.js 2:0-28 4:10-13

webpack compiled with 1 error

仔细阅读报错并查找相关资料才发现,原来是webpack5中移除了nodejs核心模块的polyfill自动引入,所以需要手动引入

OK,知道了问题所在,那就开始解决问题!

解决方案

环境依赖的安装

首先我们先安装一下node-polyfill-webpack-plugin ,这是一个 Webpack 插件,用于在浏览器环境下模拟 Node.js 核心模块的功能。

在终端执行以下命令即可,安装速度与主机的网速相关

npm install node-polyfill-webpack-plugin

其次再安装一下crypto-browserify ,这是一个兼容浏览器环境的 Node.js crypto 模块的 polyfill 库,它提供了与 Node.js 的 crypto 模块相似的功能,使你可以在浏览器环境中使用加密和哈希算法等功能。

同样在终端执行以下命令

npm install crypto-browserify

配置文件代码的修改

在你的vue.config.js文件中修改一下配置代码(直接替换即可)

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

module.exports = {
  configureWebpack: {
    resolve: {
      fallback: {
        fs: false,
        crypto: require.resolve("crypto-browserify"),
        stream: require.resolve("stream-browserify"),
      },
    },
    plugins: [new NodePolyfillPlugin()],
  },
};

操作步骤总结

上述操作中

  1. 使用了 node-polyfill-webpack-plugin 插件来解决在 Webpack 5 中缺少 Node.js 核心模块的问题。
  2. 同时,我们通过修改 resolve.fallback 配置来添加了 fscrypto 和 stream 模块的 polyfill。
  3. 其次需要确保已经安装了 node-polyfill-webpack-plugin 和 crypto-browserify 依赖。

这样修改后,重新运行打包命令,你的代码应该可以正常编译和执行!

希望有帮助到你(oi)文章来源地址https://www.toymoban.com/news/detail-848257.html

到了这里,关于Vue-解决BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Vue中启动提示polyfill缺少-webpack v5版本导致

    因为我们的项目使用webpack v5,其中polyfill Node核心模块被删除。所以,我们安装它是为了在项目中访问这些模块

    2024年02月09日
    浏览(7)
  • vue解决:You may use special comments to disable some warnings.Use // eslint-disable-next-line to ign

    vue解决:You may use special comments to disable some warnings.Use // eslint-disable-next-line to ign

    错误描述:项目启动时,出现    You may use special comments to disable some warnings.的翻译是: 你可以使用一些特殊的注释来禁用一些警告 出现这样的问题是:ESLint 对语法的要求过于严格,出现这样的问题并不是写的代码有异常,是代码的格式有问题 解决办法:取消ESLint验证规则

    2024年02月16日
    浏览(10)
  • uniapp [Vue warn]: Error in onLoad hook: “TypeError: Attempting to change the setter of an unconfigu

    用uniapp开发微信小程序时候发现报这个错误,记录下我是如何解决的! uniapp [Vue warn]: Error in onLoad hook: \\\"TypeError: Attempting to change the setter of an unconfigurable property.\\\" 由于在Object.defineproperty方法中,控制属性不可以被删除, unconfigurable 状态。 我们来看下 Object.defineproperty方法的具体

    2024年02月13日
    浏览(7)
  • Vue报错:may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore

    Vue报错:may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore

    找到项目根目录下的bulid文件夹下的 webpack.base.conf.js,找到以下代码块并注释掉第三行代码 注释完保存退出,重新启动项目即可。 关闭eslint语法检测,在 .eslintrc.js文件中 ,注释掉 eslint:recommended。 当项目并不存在build文件夹,即不属于vue脚手架工程,那请到根目录下 config文

    2024年01月23日
    浏览(8)
  • c++命名空间和include C++ #include<string> 和 using std::string

    1、C++中的命名空间namespace_51CTO博客_c++中的命名空间   2、 C++ #includestring 和 using std::string_yang20141109的博客-CSDN博客 //不光要加头文件,和C语言不同 #include utils/Errors.h //还要加using using android::status_t; using android::INVALID_OPERATION; using android::NO_ERROR; using android::BAD_VALUE; 3、c和c++的差

    2023年04月20日
    浏览(15)
  • c++命名空间和include C++ #include<string> 和 using std::string

    1、C++中的命名空间namespace_51CTO博客_c++中的命名空间   2、 C++ #includestring 和 using std::string_yang20141109的博客-CSDN博客 //不光要加头文件,和C语言不同 #include utils/Errors.h //还要加using using android::status_t; using android::INVALID_OPERATION; using android::NO_ERROR; using android::BAD_VALUE; 3、c和c++的差

    2023年04月20日
    浏览(12)
  • 解决微服务No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netfl

    一、服务启动报如下错误: 1、报错 2、报错 提示: 没有loadBalancing定义的feign客户端。忘了加入 spring-cloud-starter-loadbalancer或spring-cloud-starter-netflix-ribbon 了吗? 二、报如上错误是因为缺少负载均衡依赖导致的,引入如下依赖即可 不用写版本号,跟随 spring cloud 版本即可

    2024年02月09日
    浏览(9)
  • Eslint配置 Must use import to load ES Module(已解决)

    Eslint配置 Must use import to load ES Module(已解决)

    最近在配置前端项目时,eslint经常会碰到各种报错(灰常头疼~) Syntax Error Error No ESLint configuration found. Syntax Error: Error: D:dmqdmq-ui.eslintrc.js: Environment key “es2021” is unknown at Array.forEach () error in ./src/main.js Syntax Error: Error: Cannot find module ‘@vue/cli-plugin-babel/preset’ from ‘D:dmqdmq

    2024年02月04日
    浏览(13)
  • VNC Viewer 远程 Unable to connect to VNC Server using your chosen securitysetting. 问题解决

    VNC Viewer 远程 Unable to connect to VNC Server using your chosen securitysetting. 问题解决

    VNC Viewer 远程 Ubuntu桌面时(VNC服务端为Vino)可能出现以下问题: Unable to connect to VNC Server using your chosen security setting. Either upgrade VNC Server to a more recent version from RealVNC, or select a weaker level of encryption. 之所以出现这种问题是因为,远程连接Ubuntu桌面,在gnome桌面环境下,默认有一

    2024年02月16日
    浏览(8)
  • ESlint报错Error: Must use import to load ES Module解决方法

    适用nvm工具将node版本从 12.16.3 切换到 16.15.1

    2024年02月12日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包