32

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
  • 29,321
  • 31
  • 106
  • 146
Bishnu Bhattarai
  • 2,662
  • 7
  • 34
  • 43

7 Answers7

58

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
  • 10,121
  • 9
  • 68
  • 112
23

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,639
  • 1
  • 28
  • 36
3

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
  • 453
  • 5
  • 20
3

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
  • 4,567
  • 13
  • 30
  • 40
gilchris
  • 1,221
  • 19
  • 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.

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
  • 755
  • 7
  • 7