43

I have installed webpack using

npm install -g webpack

and

npm install webpack 

I also installed webpack-dev-server

npm install -g webpack-dev-server

After completion of installation, I ran the command webpack but, it shows below error

webpack: command not found

I am not getting what is the error.

Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
Bishnu Bhattarai
  • 2,800
  • 8
  • 36
  • 44

9 Answers9

62

Your webpack exists in ./node_modules/.bin/ folder . So you should execute this command :

./node_modules/.bin/webpack

Check out the answer in this thread .

webpack command not working

Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
25

As a good practice is recommended to install webpack and webpack-dev-server locally, more info here.

yarn add webpack webpack-dev-server --dev
# or
npm install webpack webpack-dev-server --save-dev

Then you can add these lines to your scripts section in your package.json file.

"scripts": {
  "build": "webpack --progress --colors",
  "start": "webpack-dev-server --progress --colors"
}

and finally

npm start
npm run build

Note: You need to have a webpack.config.js in the root folder to make it run correctly.

Community
  • 1
  • 1
Arnold Gandarillas
  • 3,896
  • 1
  • 30
  • 36
  • 1
    minor correction: --save but not --save-dev. You need webpack also with --production flag. – Michael A. May 01 '20 at 23:51
  • 1
    @MichaelA. This was the reason for webpack build not working on my production server. I had `webpack` and `webpack-cli` in `devDependencies` and both needed to be moved to dependencies for the build to work. – workwise Mar 20 '23 at 13:19
8

I needed to manually install:

npm install --save-dev webpack-cli

I guess its needed so that Angular CLI actually understands the commands related to Webpack.

Henry
  • 631
  • 10
  • 21
4

In ubuntu u can try sudo apt install webpack

0

If you want to use global installation, you can find webpack script in [node_installed_path]/lib/node_modules/webpack/bin/, you can use with absolute path, adding to PATH environment variable, or symbolic link, etc.

If you want to use local installation, find it in ./node_modules/.bin/.

I recommand using local installation (for same reason about babel).

double-beep
  • 5,031
  • 17
  • 33
  • 41
gilchris
  • 1,231
  • 17
  • 24
0

You need to be in proper folder to run webpack command.

What I mean by proper folder is folder in which you placed your installed module and module's package.json file.

Cause you installed it with -g parameter it is installed globally and you should find it in: ./node_modules/.bin/webpack.

Best practice is to install modules per project ( folder in which is project) not globally.

Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20
0

webpack -v: webpack command not found

node -v: v16.14.2

npm -v: 8.5.0

Tried to install webpack globally or locally and a lot of other ways to fix this issue but failed, below solution fixed my case (my case is a little bit special, I reset the prefix as below)

npm config set prefix "C:\Program Files\nodejs\npm_modules"

Solution: add the folder path xxx/npm_modules/ which included webpack.cmd to the System variable Path

enter image description here

How to find the folder path xxx/npm_modules/ which included webpack.cmd?

npm config ls

webpack.cmd in folder npm_modules, you will need this path to be added to System variable Path enter image description here

Hailin Tan
  • 989
  • 9
  • 7
0

For Linux users, sudo apt install webpack worked for me

pdutra145
  • 11
  • 2
0

I ran into this problem within an old project I returned to after a while (that used to run properly). This fixed the issue:

npx webpack

The version information is: node v18.16.1 (npm v9.5.1) and I use a MacOS.

cconsta1
  • 737
  • 1
  • 6
  • 20