我使用 create-react-app 在 React 中創建了我的專案,但過了一段時間我在控制臺中遇到了這個錯誤
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: { "path": require.resolve("path-browserify") }'
\- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "path": false }
@ ./node_modules/loader-utils/lib/index.js 7:25-54
@ ./node_modules/file-loader/dist/index.js 11:19-42
@ ./node_modules/file-loader/dist/cjs.js 3:15-33
@ ./src/App.js 13:0-35
@ ./src/index.js 7:0-24 11:33-36
看完這篇文章后,我得出的結論是我必須修改我沒有的 webpack.config.js,因為我使用 create-react-app 創建了專案。我沒有彈出(不是很推薦),而是使用了這個執行緒末尾建議的 npm 包。問題是沒有關于如何使用它的示例。我的問題是我必須遵循哪種語法來修改 config/webpack.extend.js?這是目前的代碼:
module.exports = {
dev: (config) => {
//override webpack configuration
config.externals =[..];
return config;
},
prod: (config) => {
//override webpack configuration
config.externals =[..];
return config;
}
};
我曾嘗試使用 console.log(config) 但由于錯誤被列印回來,它永遠不會被列印出來。
uj5u.com熱心網友回復:
您不能使用console.log(),因為該代碼不是從瀏覽器執行的,而是在 Webpack“編譯”階段執行的。
對于您的情況,這可能是一個可能的解決方案。
module.exports = {
dev: (config) => {
//override webpack configuration
config.resolve.fallback = {"path": require.resolve("path-browserify")};
return config;
},
prod: (config) => {
//override webpack configuration
config.resolve.fallback = {"path": require.resolve("path-browserify")};
return config;
}
}
請注意,您必須安裝該軟體包path-browserfy。
您還可以將該path屬性設定為 false。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/454069.html
標籤:javascript 反应 npm 网页包 创建反应应用
