不允许预设文件导出对象

时间:2018-03-08 21:35:16

标签: webpack babeljs

我有一个轮播文件,我希望在其中获得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.

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

10 个答案:

答案 0 :(得分:41)

You're using a combination of Babel 6 and Babel 7. There is no guarantee of compatibility across versions:

"@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",

should be

"@babel/core": "^7.0.0-beta.40",
"@babel/cli": "^7.0.0-beta.40",
"babel-loader": "^8.0.0-beta.0",
"babel-plugin-lodash": "^3.3.2",
"babel-plugin-react-transform": "^3.0.0",
"@babel/preset-react": "^7.0.0-beta.40",

and

    query: {
      presets: ['react', 'es2015'],
      plugins: ['transform-class-properties']
    }

would be

    query: {
      presets: ['@babel/react', '@babel/es2015'],
      plugins: ['@babel/proposal-class-properties']
    }

I'm also confused because you didn't mention @babel/proposal-class-properties in your package.json, but assuming it is in there it should also be updated.

答案 1 :(得分:13)

发生在我身上,对我来说,一个简单的解决方案是卸载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

答案 2 :(得分:4)

babel-loader v8开始,它们也有所变化:

webpack 4.x | babel-loader 8.x | babel 7.x

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

webpack 4.x | babel-loader 7.x | babel 6.x

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

(与@babel/preset-react相同,而不是babel-preset-react)。

答案 3 :(得分:2)

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

我更新了它,如下所示,它起到了作用:

{
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "plugins": [
        "transform-class-properties",
        "transform-object-rest-spread"
    ]
}

答案 4 :(得分:2)

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

安装

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"]

}

答案 5 :(得分:1)

这对我有用:

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

在babelrc中:

"presets" : ["es2015", "react"]    

答案 6 :(得分:1)

这是由于使用了过时的babel软件包。像大多数其他活动的Javascript项目一样,babel项目也已开始使用范围包。因此,程序包名称以@babel

开头

如果您使用的是纱线,请遵循以下内容:

第1步:删除旧软件包

$ yarn remove babel-core babel-loader babel-preset-env babel-preset-react

第2步:添加新程序包

$ yarn add -D @babel/core babel-loader @babel/preset-env @babel/preset-react

如果您使用的是npm,请遵循以下步骤:

第1步:删除旧软件包

$ npm uninstall babel-core babel-loader babel-preset-env babel-preset-react

第2步:添加新程序包

$ npm add -D @babel/core babel-loader @babel/preset-env @babel/preset-react

第3步:npm或yarn共有

在安装更新的软件包后,请记住将.babelrc的预设从react更新为@babel/preset-react。这是一个简单的例子

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

答案 7 :(得分:0)

使用以下代码替换您的.babelrc文件,这可以解决我的问题

{
  "presets": ["module:metro-react-native-babel-preset"]
}

答案 8 :(得分:0)

此解决方案对我有用:

npm install babel-loader babel-preset-react

然后在.babelrc

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

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

答案 9 :(得分:0)

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