错误:找不到相对于目录的预设“es2015”

IT技术 javascript node.js reactjs webpack
2021-03-29 05:19:03

尝试使用 babel 时出现以下错误。

错误:找不到相对于目录的预设“es2015”

webpack.config.js

module.exports = {
    entry: './main.js',
    ourput: {
        path:'./',
        filename:'index.js'
    },
    devServer:{
        inline:true,
        port:3333
    },
    module:{
        loaders:[
            {
                test:/\.js$/,
                exclude:/node_modules/,
                loader:'babel',
                query:{
                    presets:['es2015','react']
                }
            }
        ]
    }
}

包.json

{
  "name": "es6-react-setup",
  "version": "1.0.0",
  "main": "main.js",
  "dependencies": {
    "babel-core": "^6.11.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1",
    "babel-loader": "^6.2.4",
    "react": "^15.2.1",
    "react-dom": "^15.2.1",
    "webpack": "^1.13.1"
  },
  "devDependencies": {},
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "description": ""
}

终端输出 终端输出

2个回答

您需要配置 babel 以使用这些预设。您可以将此添加到您的package.json

  "babel": {
    "presets": [
      "es2015",
      "react"
    ]
  },

您也可以拥有一个.babelrc文件。

https://babeljs.io/docs/usage/babelrc/

试试这个

npm 安装 babel-preset-es2015

npm 安装 babel-preset-react

npm 安装 babel --save-dev

这对我有用。