反应错误:不允许插件/预设文件导出对象,仅导出功能

时间:2018-09-03 08:17:05

标签: javascript reactjs npm babel

我正在尝试安装新的react项目,但是遇到了一些问题。

现在,在解决了许多问题之后,我陷入了这个问题:

 ERROR in ./script.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions. In D:\react_project\node_modules\babel-preset-es2015\lib\index.js

在安装@ babel / core版本7后,我遇到了这个问题。

这是我的package.json

{
  "name": "react_project",
  "version": "1.0.0",
  "description": "first project on react",
  "main": "index.js",
  "scripts": {
    "it": "webpack-dev-server --hot"
  },
  "author": "azima",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.17.1",
    "webpack-dev-server": "^3.1.7"
  },
  "dependencies": {
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "webpack-cli": "^3.1.0"
  }
}

webpack.config.js:

var path = require('path');
module.exports = {
    entry: './script.jsx',
    output: {
        path: path.resolve(__dirname,''),
        filename: 'transpiled.js'
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loaders: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }   
            }   
        ]
    }
}

错误是什么意思,我该如何解决?

1 个答案:

答案 0 :(得分:2)

对不起,我之前没有看到这个问题,但这是问题所在和解决方案: "babel-loader": "^8.0.1""@babel/core": "^7.0.0"一起使用。 Babel 7转移到有作用域的程序包,并在插件API中引入了一些更改,因此您的问题是您将新旧Babel混在一起。

babel-preset-es2015不再需要,因为有一个@babel/preset-env可以根据目标平台处理您可能需要的所有新功能(请参阅链接以获取配置选项)。

babel-preset-react(这是babel v6的预设)现在为@babel/preset-react(v7的预设)。

现在,您的webpack加载程序配置应如下所示:

{
    test: /\.jsx?$/,
    loader: 'babel-loader',
    exclude: /node_modules/,
    options: {
        presets: ['@babel/preset-env', '@babel/preset-react']
    }
}