【问题标题】:'webpack' not recognizing when running within the project'webpack' 在项目中运行时无法识别
【发布时间】:2018-09-23 09:19:48
【问题描述】:

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

'webpack' 不是内部或外部命令、可运行程序或批处理文件。

我已经用命令安装了webpack,

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

即使我尝试了这个接受的 answer ,它也带来了另一个错误。

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

【问题讨论】:

    标签: javascript reactjs npm webpack


    【解决方案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"
      }
    }
    

    【讨论】:

    • 运行良好。谢谢
    【解决方案2】:

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

    node_modules\.bin\webpack
    

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 2021-01-28
      • 2018-04-05
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2019-01-16
      相关资源
      最近更新 更多