Error: Cannot find module ‘webpack‘

Scenario: After
configuring webpack-dev-server, when starting to use npx web-dev-serverit happily , the following error occurred:

E:\前端\nodejs\实践\js\webpack_01>npx webpack-dev-server
internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'webpack'
Require stack:
- E:\前端\nodejs\实践\js\node_modules\webpack-dev-server\bin\webpack-dev-server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (E:\前端\nodejs\实践\js\node_modules\webpack-dev-server\bin\webpack-dev-server.js:12:17)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
    
    
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'E:\\前端\\nodejs\\实践\\js\\node_modules\\webpack-dev-server\\bin\\webpack-dev-server.js'
  ]
}

The main problem: I Error: Cannot find module 'webpack'can’t find webpack??, and then I searched for a wave of information on the Internet. Everyone’s problems are all Error: Cannot find module 'webpack-cli/bin/config-yargs'. I thought it was because the error warning message was changed after the version update. Then I corrected it according to the online information. The result is still I couldn’t find webpack, I was confused...
Fortunately, I saw an article: webpack and webpack-dev-server installation configuration (solutions to various problems encountered) , although the description of the problem is different from what I have appeared, but I will try The attitude of a try, the result is really successful, the specific steps are as follows:

  1. If webpack and webpack-cli are installed globally, they need to be installed once locally in the current project
npm i -s webpack webpack-cli
  1. At this time, if you run npx webpack-dev-serverit again , there will be problems that often appear on the Internet:, the Error: Cannot find module 'webpack-cli/bin/config-yargs'solution is everywhere on the Internet, reduce it webpack-cli版本, or use it to npx webpack serverun, etc. I use npx webpack serveit to run it for the sake of convenience .

Guess you like

Origin blog.csdn.net/chen__cheng/article/details/115264471