Borislav Hadzhiev
Fri Mar 25 2022·2 min read
Photo by Clay Banks
To solve the "Cannot find module 'webpack-cli'" error, make sure to install
webpack-cli
globally by running the npm i -g webpack-cli
command and create
a symbolic link from the globally-installed package to node_modules
by running
the npm link webpack-cli
command.
Open your terminal in your project's root directory (where your package.json
file is located) and run the following commands:
# ✅ Install webpack-cli globally npm install -g webpack webpack-cli # ✅ Create a symbolic link from the global package # to node_modules/ of current folder npm link webpack npm link webpack-cli
Once you run the two commands, the error should be resolved.
sudo
.# 👇️ If you got permissions error, run with sudo sudo npm install -g webpack webpack-cli npm link webpack npm link webpack-cli
The npm link command creates
a symbolic link from the globally installed package to the node_modules/
directory of the current folder.
webpack
and webpack-cli
locally.Open your terminal in your project's root directory (where your package.json
file is located) and run the following commands:
npm install --save-dev webpack webpack-cli
This will add webpack-cli
to the development dependencies of your project, so
you don't have to run the link
command.
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.
If you're still getting the "Cannot find module 'webpack-cli'" error, open your
package.json
file and make sure it contains the webpack-cli
package in the
devDependencies
object.
{ // ... rest "devDependencies": { "webpack": "^5.70.0", "webpack-cli": "^4.9.2" // ... rest } }
You can try to manually add the line and re-run npm install
.
npm install