报错:找不到模块'webpack/bin/config-yargs',webpack版本切换后仍未解决
Hey there, let's get that frustrating module error sorted out! This issue almost always boils down to a version mismatch between Webpack and its companion tools like webpack-cli or webpack-dev-server—especially since you're working with older Webpack versions (2.x/3.x) which don't play nice with modern CLI releases.
Here's a step-by-step fix:
1. Clean up conflicting dependencies first
First, let's remove any existing Webpack-related packages that might be causing conflicts. Run this in your project root:
npm uninstall webpack webpack-cli webpack-dev-server --save-dev
(If you use Yarn instead, swap npm uninstall with yarn remove and keep the rest.)
2. Install version-matched packages
Pick the set that matches the Webpack version you want to use:
For Webpack 3.10.0
Webpack 3 requires webpack-cli@2.x (newer CLI versions dropped support for the old config-yargs module). Install them together:
npm install webpack@3.10.0 webpack-cli@2.1.4 --save-dev
If you use webpack-dev-server, install a compatible version too:
npm install webpack-dev-server@2.9.7 --save-dev
For Webpack 2.3.0
Webpack 2 needs an even older CLI version—go with webpack-cli@1.x:
npm install webpack@2.3.0 webpack-cli@1.5.1 --save-dev
And the matching dev-server:
npm install webpack-dev-server@2.4.2 --save-dev
3. Test your setup
Run your usual Webpack command (like webpack --config webpack.config.js or your npm script, e.g., npm run build)—the error should be gone now!
A quick heads-up: Double-check your package.json scripts to make sure you're using parameters that your Webpack version supports. Older versions have slightly different command syntax compared to modern Webpack.
内容的提问来源于stack exchange,提问作者Elder

