Webpack-Babel错误-找不到相对于目录的预设“ @ babel / env”

时间:2019-01-20 13:20:51

标签: webpack react-redux babel babel-loader

我是开发人员新手。在尝试构建我的应用程序时,出现以下错误。我引用了:Similar Issue,但无法解决:

Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Couldn't find preset "@babel/env" relative to directory "/home/jdev/www/react-redux-node-elasticsearch"
at /home/jdev/www/react-redux-node-elasticsearch/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19

这是我的package.json

"dependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-env": "^2.4.1",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "express": "^4.16.4",
    "pug": "^2.0.3",
    "webpack": "^4.28.4"
  },
  "devDependencies": {
    "webpack-cli": "^3.2.1"
  }

这是.babelrc的内容

{
  "presets": [
      "babel-env",
      "babel-preset-env",
      "babel-preset-react"
  ]
}

有人可以协助配置吗?对于像我这样的新手来说,这非常令人困惑,而且我在线上看过多个问题,但我无法解决。我想提前得到您的时间和帮助。

1 个答案:

答案 0 :(得分:0)

Jeff,您是对的,这是一个版本控制问题。目前,这可能是您可以接受的一项工作。

在package.json文件中,将代码修改为以下依赖版本:

"dependencies": {
  "babel": "^6.5.2",
  "babel-cli": "^6.18.0",
  "babel-core": "^6.21.0",
  "babel-loader": "^6.2.10",
  "babel-preset-es2015": "^6.18.0",
  "babel-preset-react": "^6.16.0",
  "express": "^4.14.0",
  "pug": "^2.0.0-beta6",
  "react": "^15.4.2",
  "react-dom": "^15.4.2",
  "webpack": "^1.14.0"
}

您的.babelrc文件看起来不错。

此外,在webpack.config.js文件中添加以下内容,以确保此处的配置也正确:

module.exports = {
  entry: "./path/to/your/app.js",
  output: {
    path: __dirname + "/src/js",
    filename: "bundle.min.js"
},
module: {
    loaders: [{
        exclude: /(node_modules)/,
        loader: "babel",
        query: {
            presets: ["es2015", "react"]
        }
    }]
},
  watch: true
}