解决报错Cannot find module 'webpack'问题

文章目录

          • 1.全局安装webpack
          • 2.进入项目目录 link webpack
          • 3.再次打包运行项目 npm run start发现报错
          • 4.安装extract-text-webpack-plugin
          • 5.安装webpack-merge
          • 6.安装copy-webpack-plugin
          • 7.安装html-webpack-plugin
          • 8.安装friendly-errors-webpack-plugin
          • 9.那就继续安装node-notifier
          • 10.安装 npm install --save

使用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  

再次运行项目发现还有一个问题:
解决报错Cannot find module 'webpack'问题_第1张图片

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

最后重新开一个终端运行项目就成功了.
解决报错Cannot find module 'webpack'问题_第2张图片

你可能感兴趣的:(webpack)