问题:
在VUE3+WebPack >5开发时遇到Module not found: Error: Can’t resolve ‘crypto’错误,报错如下:
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 }
原因是由于在webpack5中移除了nodejs核心模块的polyfill自动引入,所以需要手动引入
解决方案
1.在package.json文件中的”dependencies”添加“node-polyfill-webpack-plugin”,或
npm install node-polyfill-webpack-plugin
2.在vue.config.json中添加
//头部引用 const NodePolyfillPlugin = require('node-polyfill-webpack-plugin') //加入 configureWebpack: { plugins: [new NodePolyfillPlugin()] }
完整文件如下:
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin') module.exports = defineConfig({ configureWebpack: { plugins: [new NodePolyfillPlugin()] } })
再次运行,问题解决