webpack: command not found error [Solved]

avatar

Borislav Hadzhiev

Last updated: Mar 12, 2022

banner

Photo from Unsplash

webpack: command not found error [Solved] #

Use npx to solve the error "webpack: command not found", e.g. npx webpack or install the package locally by running npm install --save-dev webpack webpack-cli to be able to use the command in the scripts section of your package.json file.

command not found webpack

The fastest way to solve the error is to use the npx command.

shell
npx webpack npx webpack --version

Alternatively, you can install webpack locally. Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

shell
npm install --save-dev webpack webpack-cli

This will add webpack to the development dependencies of your project.

Now you can use the webpack command in the scripts section of your package.json file.

package.json
{ "scripts": { "build": "webpack --config webpack.config.js" } }

Now you would run the command as npm run build and not use webpack directly in your terminal.

If the error is not resolved, you can try to install webpack globally. Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

shell
# ✅ Install Webpack globally npm install -g webpack webpack-cli # ✅ Create a symbolic link from the global package # to node_modules/ of current folder npm link webpack

The npm link command creates a symbolic link from the globally installed package to the node_modules/ directory of the current folder.

Once you run the two commands, the error should be resolved.

If the global installation of webpack fails, you might have to run the command prefixed with sudo.
shell
# 👇️ If you got permissions error, run with sudo sudo npm install -g webpack webpack-cli npm link webpack

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.

shell
# 👇️ 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 persists. VSCode often glitches and a reboot solves things sometimes.

If you're still getting the "webpack: command not found" error, open your package.json file and make sure it contains the webpack package in the devDependencies object.

package.json
{ // ... rest "devDependencies": { "webpack": "^5.71.0", "webpack-cli": "^4.9.2" // ... rest } }

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

shell
npm install

Or install the latest version of the packages:

shell
npm install --save-dev webpack@latest webpack-cli@latest

Conclusion #

Use npx to solve the error "webpack: command not found", e.g. npx webpack or install the package locally by running npm install --save-dev webpack webpack-cli to be able to use the command in the scripts section of your package.json file.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.