webpack is not recognized as a internal or externa

2020-01-26 04:17发布

I am Learning React.js and i am using windows 8 OS.i have navigate to my root folder

1.Created the package.json file by npm init
2. install webpack by npm install -S webpack.now webpack has been downloaded to my modules folder
3. install webpack globally by typing npm install webpack -g
4. i am also having a webpack.config.js in my root folder which contains the source and ouput directory
5. when i type the webpack command i am getting the below error.

webpack is not recognized as a internal or external command,operable program or batch file

标签: npm webpack
22条回答
啃猪蹄的小仙女
2楼-- · 2020-01-26 04:48

We also experienced this problem and I like all the answers that suggest using a script defined in package.json.

For our solutions we often use the following sequence:

  1. npm install --save-dev webpack-cli (if you're using webpack v4 or later, otherwise use npm install --save-dev webpack, see webpack installation, retrieved 19 Jan 2019)
  2. npx webpack

Step 1 is a one-off. Step 2 also checks ./node_modules/.bin. You can add the second step as a npm script to package.json as well, for example:

{
   ...
   "scripts": {
      ...
      "build": "npx webpack --mode development",
      ...
   },
   ...
}

and then use npm run build to execute this script.

Tested this solution with npm version 6.5.0, webpack version 4.28.4 and webpack-cli version 3.2.1 on Windows 10, executing all commands inside of a PowerShell window. My nodejs version is/was 10.14.2. I also tested this on Ubuntu Linux version 18.04.

I'd advise against installing webpack globally, in particular if you are working with a lot of different projects each of which may require a different version of webpack. Installing webpack globally locks you down to a particular version across all projects on the same machine.

查看更多
放我归山
3楼-- · 2020-01-26 04:49

Install WebPack globally

npm install --global webpack
查看更多
手持菜刀,她持情操
4楼-- · 2020-01-26 04:50

Add webpack command as an npm script in your package.json.

{
    "name": "react-app",
    "version": "1.0.0",
    "scripts": {
      "compile": "webpack --config webpack.config.js"
    }
}

Then run

npm run compile

When the webpack is installed it creates a binary in ./node_modules/.bin folder. npm scripts also looks for executable created in this folder

查看更多
男人必须洒脱
5楼-- · 2020-01-26 04:52

I got the same error, none of the solutions worked for me, I reinstalled node and that repaired my environment, everything works again.

查看更多
登录 后发表回答