解決報錯Cannot find module 'webpack'問題


使用webpack打包運行vue項目時報錯:
報錯:

module.js:549
throw err;
^
Error: Cannot find module ‘webpack’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/usr/local/lib/node_modules/webpack-dev-server/bin/webpack-dev-server.js:12:17)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

解決方案:

1.全局安裝webpack
sudo npm install -g webpack   
2.進入項目目錄 link webpack
sudo npm link webpack
3.再次打包運行項目 npm run start發現報錯

Cannot find module ‘extract-text-webpack-plugin’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/home/galaxy/桌面/xiangmu/yi/build/utils.js:4:27)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

原來是缺少webpack-plugin插件.那麼就缺啥裝啥.讀者可以根據自己報錯缺少的插件進行安裝.
以下是本人在這個過程中的報錯情況和解決辦法.

4.安裝extract-text-webpack-plugin
 sudo npm install extract-text-webpack-plugin

再次運行項目報錯:

Cannot find module ‘webpack-merge’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/home/galaxy/桌面/xiangmu/yi/build/webpack.dev.conf.js:5:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

5.安裝webpack-merge
sudo npm install webpack-merge 

再次運行項目報錯:

Cannot find module ‘copy-webpack-plugin’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/home/galaxy/桌面/xiangmu/yi/build/webpack.dev.conf.js:8:27)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)

6.安裝copy-webpack-plugin
sudo npm install copy-webpack-plugin 

運行項目報錯:

Cannot find module ‘html-webpack-plugin’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/home/galaxy/桌面/xiangmu/yi/build/webpack.dev.conf.js:9:27)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at requireConfig (/home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:97:18)
at /home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:104:17
at Array.forEach ()
at module.exports (/home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:102:15)

7.安裝html-webpack-plugin
sudo npm install html-webpack-plugin  

再次運行項目報錯:

Cannot find module ‘friendly-errors-webpack-plugin’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/home/galaxy/桌面/xiangmu/yi/build/webpack.dev.conf.js:10:30)
at Module._compile (module.js:652:30)
at Object.Module._extensions…js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at requireConfig (/home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:97:18)
at /home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:104:17
at Array.forEach ()
at module.exports (/home/galaxy/桌面/xiangmu/yi/node_modules/webpack/bin/convert-argv.js:102:15)

8.安裝friendly-errors-webpack-plugin
sudo npm install friendly-errors-webpack-plugin 

再次運行項目發現還是報錯

Cannot find module ‘node-notifier’
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.exports.createNotifierCallback (/home/galaxy/桌面/xiangmu/yi/build/utils.js:86:20)
at portfinder.getPort (/home/galaxy/桌面/xiangmu/yi/build/webpack.dev.conf.js:88:17)
at /home/galaxy/桌面/xiangmu/yi/node_modules/portfinder/lib/portfinder.js:196:16
at /home/galaxy/桌面/xiangmu/yi/node_modules/async/dist/async.js:473:16
at replenish (/home/galaxy/桌面/xiangmu/yi/node_modules/async/dist/async.js:1006:25)
at iterateeCallback (/home/galaxy/桌面/xiangmu/yi/node_modules/async/dist/async.js:995:17)
at /home/galaxy/桌面/xiangmu/yi/node_modules/async/dist/async.js:969:16
at /home/galaxy/桌面/xiangmu/yi/node_modules/portfinder/lib/portfinder.js:157:16
at Server.onListen (/home/galaxy/桌面/xiangmu/yi/node_modules/portfinder/lib/portfinder.js:43:7)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Server.emit (events.js:208:7)

9.那就繼續安裝node-notifier
 sudo npm install node-notifier  

再次運行項目發現還有一個問題:
在這裏插入圖片描述

10.安裝 npm install --save
sudo npm install --save 

最後重新開一個終端運行項目就成功了.
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章