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

时间:2018-04-13 05:20:53

标签: javascript reactjs npm webpack

当我尝试在项目中运行'webpack'时,命令行显示以下错误:

  

'webpack'未被识别为内部或外部命令,可操作程序或批处理文件。

我已经使用命令

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

即使我尝试了这个接受answer,它还会出现另一个错误。 enter image description here

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文件

{
  "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"
  }
    }

使用操作系统 - Windows 8, npm版本 - 5.6.0

2 个答案:

答案 0 :(得分:2)

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

node_modules\.bin\webpack

答案 1 :(得分:1)

根据您需要更新规则的屏幕截图,我已更新配置文件,其工作正常。请仔细检查。

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

{
  "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"
  }
}