Babel 错误:插件/预设文件不允许导出对象,只有函数

Posted

技术标签:

【中文标题】Babel 错误:插件/预设文件不允许导出对象,只有函数【英文标题】:Babel Error: Plugin/Preset files are not allowed to export objects, only functions 【发布时间】:2019-06-09 06:58:18 【问题描述】:

我正在尝试在 expo 安装上运行故事书。所以到目前为止我只在应用程序中获得了展览和故事书。

我无法启动网络故事书。

我的 package.json 看起来像这样:

"dependencies": 
  "expo": "^32.0.0",
  "react": "16.5.0",
  "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz"
,
"devDependencies": 
  "@babel/core": "^7.2.2",
  "@babel/preset-env": "^7.2.3",
  "@babel/preset-react": "^7.0.0",
  "@babel/runtime": "^7.2.0",
  "@storybook/addon-actions": "^4.1.6",
  "@storybook/addon-knobs": "^4.1.6",
  "@storybook/addon-links": "^4.1.6",
  "@storybook/addon-ondevice-knobs": "^4.1.6",
  "@storybook/addon-ondevice-notes": "^4.1.6",
  "@storybook/addons": "^4.1.6",
  "@storybook/react": "^4.1.6",
  "@storybook/react-native": "^4.1.6",
  "babel-loader": "^7",
  "babel-preset-expo": "^5.0.0",
  "prop-types": "^15.6.2",
  "react-dom": "16.5.0"
,

我的 babel.config.js 是这样的;

module.exports = function(api) 
  api.cache(true);
  return 
    presets: ['babel-preset-expo'],
  ;
;

我也尝试过:

module.exports = function(api) 
  api.cache(true);
  return 
    presets: ["@babel/env", "@babel/react"],
  ;
;

尝试运行故事书时出现此错误:

ERROR in ./storybook/addons.js
Module build failed (from ./node_modules/@storybook/react-native/node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions. In /Users/kristoffernielsen/repos/app/storybook/node_modules/babel-preset-react/lib/index.js
    at createDescriptor (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
    at items.map (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPresetDescriptors (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
    at passPerPreset (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:58:96)
    at cachedFunction (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/caching.js:33:19)
    at presets.presets (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-descriptors.js:29:84)
    at mergeChainOpts (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-chain.js:320:26)
    at /Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-chain.js:283:7
    at buildRootChain (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/config-chain.js:68:29)
    at loadPrivatePartialConfig (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/partial.js:85:55)
    at Object.loadPartialConfig (/Users/kristoffernielsen/repos/app/storybook/node_modules/@babel/core/lib/config/partial.js:110:18)
    at Object.<anonymous> (/Users/kristoffernielsen/repos/app/storybook/node_modules/@storybook/react-native/node_modules/babel-loader/lib/index.js:140:26)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/kristoffernielsen/repos/app/storybook/node_modules/@storybook/react-native/node_modules/babel-loader/lib/index.js:3:103)
 @ multi ./storybook/addons.js ./node_modules/@storybook/react-native/dist/manager/index.js manager[0]

即使我的 addons.js 是空的,它也会失败。

【问题讨论】:

【参考方案1】:

这对你没有帮助。 你仍然需要你的 addon.js 文件。

看这里:https://github.com/storybooks/storybook/issues/5249

我认为你需要等待。

https://github.com/storybooks/storybook/issues/5249#issuecomment-472179512

【讨论】:

【参考方案2】:

我已经修复了这个重命名删除或重命名文件到 rn-addons.js

【讨论】:

您能更清楚地知道要删除和重命名哪个文件吗?【参考方案3】:

只需将 .babelrc 添加到您的故事书文件夹中,并使用默认的 react native 或 expo 预设。

This 为我工作:


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

【讨论】:

以上是关于Babel 错误:插件/预设文件不允许导出对象,只有函数的主要内容,如果未能解决你的问题,请参考以下文章

插件/预设文件不允许导出对象,只有功能

预设文件不允许导出对象

Babel 7 失败,单个插件说“检测到重复的插件/预设”。

用 Babel 去构建 TypeScript 项目

babel的插件

babel入门&文件转译