@vue/cli4升級@vue/cli5?node.js?polyfills錯誤的解決方式
一、錯誤描述
因前端項目做的少,今天用 vue腳手架創(chuàng)建項目選擇了 @vue/cli 5.0 版本,在編譯項目時出現(xiàn)如下錯誤:
二、錯誤日志
1、日志內(nèi)容:
錯誤1:
error in ./node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in 'H:\iWork\產(chǎn)品代碼\前端代碼\ui_2\node_modules\jwa'
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 }
錯誤2:
error in ./node_modules/jwa/index.js
Module not found: Error: Can't resolve 'util' in 'H:\iWork\產(chǎn)品代碼\前端代碼\ui_2\node_modules\jwa'
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: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }
錯誤3:
error in ./node_modules/jws/lib/data-stream.js
Module not found: Error: Can't resolve 'stream' in 'H:\iWork\產(chǎn)品代碼\前端代碼\ui_2\node_modules\jws\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: { "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 }
2、錯誤原因
因為 @vue/cli 從 4.0 升級到 5.0 已經(jīng)不默認包含 node.js 的核心 polyfills 組件了。項目的 package.json 的版本如下圖:

3、解決方法[可以使用的方法]
參考資料:
中文Webpack相關(guān)介紹:解析(Resolve) | webpack 中文文檔
node-polyfill-webpack-plugin 只能在 Webpack 5+ 版本使用,相關(guān)介紹:Package - node-polyfill-webpack-plugin (npmmirror.com)
1.執(zhí)行安裝命令:npm install node-polyfill-webpack-plugin
2.進行配置 vue.config.js
在頂行添加如下:const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
在 defineConfig 中添加節(jié)點如下:
configureWebpack: {
resolve: {
alias: {},
fallback: {
//其他的如果不啟用可以用 keyname :false,例如:crypto:false,
"crypto": require.resolve("crypto-browserify"),
"stream": require.resolve("stream-browserify")
},
},
plugins: [new NodePolyfillPlugin()]
}
特殊說明:fallback 內(nèi)的配置好像并未生效,不過還是推薦您寫上你缺失的組件名。如下圖:

4、解決辦法[存在問題,需要研究還]
通過對日志的分析因為有其他組件引用到了 polyfills 的核心組件并沒有安裝,所以報錯了,這里需要執(zhí)行 npm install 命令進行包安裝即可。
命令:
npm install util stream-browserify crypto-browserify
包安裝完成后 package.json 如下圖:

總結(jié)
到此這篇關(guān)于@vue/cli4升級@vue/cli5 node.js polyfills錯誤解決的文章就介紹到這了,更多相關(guān)@vue/cli4升級@vue/cli5錯誤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中監(jiān)聽input框獲取焦點及失去焦點的問題
這篇文章主要介紹了vue中監(jiān)聽input框獲取焦點,失去焦點的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
關(guān)于element-ui日期時間選擇器選不中12小時以后的時間詳解
在之前做個一個組件頁面中,引用了element-ui組件的日期選擇器,遇到的一個小問題,下面這篇文章主要給大家介紹了關(guān)于element-ui日期時間選擇器選不中12小時以后時間的相關(guān)資料,需要的朋友可以參考下2022-08-08
去除Element-Plus下拉菜單邊框的實現(xiàn)步驟
Element-Plus 是 Element UI 的 Vue 3 版本,它提供了一套完整的組件庫,在使用 Element-Plus 進行開發(fā)時,我們可能會遇到需要自定義組件樣式的情況,本文將介紹如何使用 CSS 來去除 Element-Plus 下拉框的邊框,需要的朋友可以參考下2024-03-03
vue-element-admin?登陸及目錄權(quán)限控制的實現(xiàn)
本文主要介紹了vue-element-admin?登陸及目錄權(quán)限控制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
Vue中commit和dispatch區(qū)別及用法辨析(最新)
在Vue中,commit和dispatch是兩個用于觸發(fā)Vuex store中的mutations和actions的方法,這篇文章主要介紹了Vue中commit和dispatch區(qū)別及其用法辨析,需要的朋友可以參考下2024-06-06
vue+element-ui動態(tài)生成多級表頭的方法
今天小編就為大家分享一篇vue+element-ui動態(tài)生成多級表頭的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

