前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BREAKING CHANGE: webpack<5 used to include polyfills for node.js

BREAKING CHANGE: webpack<5 used to include polyfills for node.js

作者头像
孟斯特
发布2024-05-09 16:53:20
4420
发布2024-05-09 16:53:20
举报
文章被收录于专栏:code人生code人生

问题

在使用vue 3运行web3钱包项目时,突然出现以下报错信息:

代码语言:javascript
复制
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: { "crypto": require.resolve("crypto-browserify") }'
    - install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "crypto": false }
 @ ./node_modules/ethereumjs-wallet/dist.browser/index.js 175:19-42
 @ ./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/components/WalletsAPI.vue?vue&type=script&setup=true&lang=js 3:0-42 21:19-39 52:15-20
 @ ./src/components/WalletsAPI.vue?vue&type=script&setup=true&lang=js 1:0-213 1:0-213 1:214-416 1:214-416
 @ ./src/components/WalletsAPI.vue 2:0-72 3:0-67 3:0-67 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&setup=true&lang=js 1:0-53 9:6-16
 @ ./src/App.vue?vue&type=script&setup=true&lang=js 1:0-200 1:0-200 1:201-390 1:201-390
 @ ./src/App.vue 2:0-65 3:0-60 3:0-60 6:49-55
 @ ./src/main.js 2:0-28 3:10-13

根据上面的提示,webpack5默认移除了node.js的核心模块,要使用的话需要手动引入。

解决办法

首先我们需要先按照下面的命令安装一个插件node-polyfill-webpack-plugin,用于在浏览器环境下模拟nodejs核心模块的功能:

代码语言:javascript
复制
$ npm i node-polyfill-webpack-plugin

然后再安装crypto-browserify

代码语言:javascript
复制
$ npm i crypto-browserify

最后在修完我们项目中的vue.config.js文件

代码语言:javascript
复制
const { defineConfig } = require('@vue/cli-service')
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")  // add
module.exports = defineConfig({
  transpileDependencies: true,
  configureWebpack:{                                                // add
    plugins:[new NodePolyfillPlugin()]
  }
})

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[1]进行许可,使用时请注明出处。 Author: mengbin[2] blog: mengbin[3] Github: mengbin92[4] cnblogs: 恋水无意[5] 腾讯云开发者社区:孟斯特[6]

References

[1] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh [2] mengbin: mengbin1992@outlook.com [3] mengbin: https://mengbin.top [4] mengbin92: https://mengbin92.github.io/ [5] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/ [6] 孟斯特: https://cloud.tencent.com/developer/user/6649301

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 孟斯特 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 解决办法
    • References
    相关产品与服务
    云开发 CloudBase
    云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档