@vue/cli4升級(jí)@vue/cli5?node.js?polyfills錯(cuò)誤的解決方式
一、錯(cuò)誤描述
因前端項(xiàng)目做的少,今天用 vue腳手架創(chuàng)建項(xiàng)目選擇了 @vue/cli 5.0 版本,在編譯項(xiàng)目時(shí)出現(xiàn)如下錯(cuò)誤:
二、錯(cuò)誤日志
1、日志內(nèi)容:
錯(cuò)誤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 }
錯(cuò)誤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 }
錯(cuò)誤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、錯(cuò)誤原因
因?yàn)?@vue/cli 從 4.0 升級(jí)到 5.0 已經(jīng)不默認(rèn)包含 node.js 的核心 polyfills 組件了。項(xiàng)目的 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.進(jìn)行配置 vue.config.js
在頂行添加如下:const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')
在 defineConfig 中添加節(jié)點(diǎn)如下:
configureWebpack: {
resolve: {
alias: {},
fallback: {
//其他的如果不啟用可以用 keyname :false,例如:crypto:false,
"crypto": require.resolve("crypto-browserify"),
"stream": require.resolve("stream-browserify")
},
},
plugins: [new NodePolyfillPlugin()]
}
特殊說(shuō)明:fallback 內(nèi)的配置好像并未生效,不過(guò)還是推薦您寫(xiě)上你缺失的組件名。如下圖:

4、解決辦法[存在問(wèn)題,需要研究還]
通過(guò)對(duì)日志的分析因?yàn)橛衅渌M件引用到了 polyfills 的核心組件并沒(méi)有安裝,所以報(bào)錯(cuò)了,這里需要執(zhí)行 npm install 命令進(jìn)行包安裝即可。
命令:
npm install util stream-browserify crypto-browserify
包安裝完成后 package.json 如下圖:

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

