预设文件不允许导出对象

我有一个要在其中index.js建立的轮播文件block.build.js,所以我webpack.config.js是:

var config = {
  entry: './index.js',
  output: {
    path: __dirname,
    filename: 'block.build.js',
  },
  devServer: {
    contentBase: './Carousel'
  },
  module : {
    rules : [
      {
        test: /.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['react', 'es2015'],
          plugins: ['transform-class-properties']
        }
      }
    ]
  }
};
module.exports = config;

package.json我使用低于:

{
  "name": "carousel",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "dependencies": {
    "@babel/core": "^7.0.0-beta.40",
    "babel-cli": "^6.26.0",
    "babel-loader": "^8.0.0-beta.0",
    "babel-plugin-lodash": "^3.3.2",
    "babel-plugin-react-transform": "^3.0.0",
    "babel-preset-react": "^6.24.1",
    "cross-env": "^5.1.3",
    "lodash": "^4.17.5",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-swipeable": "^4.2.0",
    "styled-components": "^3.2.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack --watch",
    "start": "webpack-dev-server --open",
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.10",
    "webpack-dev-server": "^3.1.0"
  },
  "author": "brad traversy",
  "license": "ISC"
}

…但是我收到此错误消息:

ERROR in ./index.js
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.

有谁知道如何解决这个问题?

米亚凯2020/03/18 17:10:42

我在.babelrc的预设中包含“ stage-1”,因此删除了该错误,错误消失了

伽罗GO2020/03/18 17:10:42

将您的.babelrc文件替换为以下代码,可解决我的问题

{
  "presets": ["module:metro-react-native-babel-preset"]
}
阿飞理查德神奇2020/03/18 17:10:42

此解决方案为我工作:

npm install babel-loader babel-preset-react

然后在 .babelrc

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

然后运行npm run start,webpack将生成dist目录。

飞云小小猪猪2020/03/18 17:10:42

babel 7从版本6开始有升级,请参阅https://babeljs.io/docs/en/v7-migration解决当前的问题/错误

安装

npm install --save-dev @babel/preset-react

npm install --save-dev @babel/preset-env

然后在.babelrc中,预设的依赖项应类似于

{

"presets":["@babel/preset-env", "@babel/preset-react"],

   "plugins": [
    "react-hot-loader/babel", "transform-object-rest-spread"]

}
村村Gil2020/03/18 17:10:42

我的webpack / react项目中遇到了相同的问题-似乎.babelrc文件有问题

我更新了它,如下图所示,它成功了:

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        "transform-class-properties",
        "transform-object-rest-spread"
    ]
}
神乐小宇宙斯丁2020/03/18 17:10:42

这为我工作:

npm uninstall --save babel-loader
npm uninstall --save @babel/core
npm install --save-dev babel-loader@^7

在babelrc中:

"presets" : ["es2015", "react"]    
梅Eva2020/03/18 17:10:42

它发生在我身上,对我来说,一个简单的解决方案是卸载babel-loader@8^@babel/core

npm uninstall --save babel-loader
npm uninstall --save @babel/core

…然后安装版本7 babel-loader:

npm install --save-dev babel-loader@^7
番长古一GO2020/03/18 17:10:42

同样从babel-loaderv8版本开始,它们有所变化:

webpack 4.x | babel-loader 8.x | 巴别塔7.x

npm install -D babel-loader @babel/core @babel/preset-env webpack

webpack 4.x | babel-loader 7.x | 通天塔6.x

npm install -D babel-loader@7 babel-core babel-preset-env webpack

(对于同样的事情@babel/preset-react,而不是babel-preset-react)。