Solve - Cannot find module 'webpack/bin/config-yargs' error


Borislav Hadzhiev

Fri Mar 25 20222 min read


Photo by Clay Banks

Solve - Cannot find module 'webpack/bin/config-yargs' error #

To solve the error "Cannot find module 'webpack/bin/config-yargs'", make sure you're running a recent version of webpack and replace the webpack-dev-server command with webpack serve in your package.json file.

Open your package.json file and make sure your webpack command starts with webpack serve, and NOT webpack-dev-server.

{ "scripts": { "dev": "webpack serve --config --progress", // ... rest }, // ... rest }

The webpack team changed the command from webpack-dev-server to webpack serve starting version 4 of webpack.

If the error is not resolved, try installing webpack, webpack-cli and webpack-dev-server locally.

Open your terminal in your project's root directory (where your package.json file is located) and run the following command:

npm install --save-dev webpack webpack-cli webpack-dev-server

This will add the webpack-dev-server package to the development dependencies of your project.

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ delete dist or build (the directory where webpack outputs files) rm -rf dist # 👇️ clean npm cache npm cache clean --force npm install

Make sure to also delete your dist or build folder if you have one. This is where webpack outputs files.

Make sure to restart your IDE if the error still persists. VSCode often glitches and a reboot solves things sometimes.

If you're still getting the "Cannot find module 'webpack/bin/config-yargs'" error, open your package.json file and make sure it contains the webpack package in the devDependencies object and is version 5 or more recent.

{ // ... rest "devDependencies": { "webpack": "^5.70.0", "webpack-cli": "^4.9.2", "webpack-dev-server": "^4.7.4" // ... rest } }

You can try to manually add the packages and re-run npm install.

npm install

Alternatively, you could install the latest version of the packages by running:

npm install --save-dev webpack@latest webpack-cli@latest webpack-dev-server@latest
Use the search field on my Home Page to filter through my more than 1,000 articles.