【问题标题】:error on react: Plugin/Preset files are not allowed to export objects, only functions反应错误:插件/预设文件不允许导出对象,只有功能
【发布时间】:2019-02-08 06:34:42
【问题描述】:

我正在尝试安装新的 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']
                }   
            }   
        ]
    }
}

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

【问题讨论】:

    标签: javascript reactjs npm babeljs


    【解决方案1】:

    对不起,我之前没有看到这个问题,但这里是问题和解决方案: "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']
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2019-06-09
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      相关资源
      最近更新 更多