简体   繁体   English

“webpack”在项目中运行时无法识别

[英]'webpack' not recognizing when running within the project

When I try to run 'webpack' within the project the command line displays the following error:当我尝试在项目中运行“webpack”时,命令行显示以下错误:

'webpack' is not recognized as an internal or external command, operable program or batch file. 'webpack' 不是内部或外部命令,也不是可运行的程序或批处理文件。

I have installed webpack with the command,我已经使用命令安装了 webpack,

npm install webpack babel-core babel loader babel-preset-es2015 babel-preset-react react react-dom --save

Even though I tried this accepted answer , it comes with another error.即使我尝试了这个已接受的答案,它也带来了另一个错误。 在此处输入图像描述

webpack.config.js file webpack.config.js文件

module.exports = {
entry: './src/index.js',
output: {
    path: './dist',
    filename: 'bundle.js'
},
module: {
    loaders: [{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
            presets: ['es2015', 'react']
        }
    }]
}}

package.json file package.json 文件

{
  "name": "dummytextgen",
  "version": "1.0.0",
  "description": "Simple dummy text generator",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "compile": "webpack --config webpack.config.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "webpack": "^4.5.0"
  },
  "devDependencies": {
    "webpack-cli": "^2.0.14"
  }
    }

Using OS - Windows 8, npm version - 5.6.0使用操作系统 - Windows 8、npm 版本 - 5.6.0

当您尝试从项目文件夹中执行weback时,您可能会尝试提供webpack二进制文件的确切路径:

node_modules\.bin\webpack

As per the screenshot you need to update the rules ,I have updated the configuration file its working fine. 根据屏幕快照,您需要更新规则,我已经更新了配置文件,可以正常工作。 Please go through. 请通过。

webpack.config.js webpack.config.js

var path=require('path')
    module.exports = {
        entry: './src/index.js',
        output: {
            path: __dirname + "/dist",
            filename: 'bundle.js'
        },
        module: {
            rules: [{
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react']
                }
            }]
        }}

package.json 的package.json

{
  "name": "dummytextgen",
  "version": "1.0.0",
  "description": "Simple dummy text generator",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "compile": "webpack --config webpack.config.js --mode development"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^16.3.1",
    "react-dom": "^16.3.1",
    "webpack": "^4.5.0"
  },
  "devDependencies": {
    "webpack-cli": "^2.0.14"
  }
}
npm install webpack webpack-cli -g

should help应该帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM