[英]Getting "Error: Plugin/Preset files are not allowed to export objects, only functions." from babel-preset-react-app/index.js
So I have a project that was bootstrapped with create-react-app
and when trying to build with react-scripts build
, I receive the following error output:所以我有一个使用create-react-app
引导的项目,当尝试使用react-scripts build
,我收到以下错误输出:
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.
According to the output, the error is coming from node_modules/babel-preset-react-app/index.js
which looks like the following:根据输出,错误来自node_modules/babel-preset-react-app/index.js
,如下所示:
'use strict';
const create = require('./create');
var env = process.env.BABEL_ENV || process.env.NODE_ENV;
module.exports = create(env);
I am using Babel 7 and below is a list of the relevant dependency packages from package.json
:我正在使用 Babel 7,以下是package.json
相关依赖包的列表:
"dependencies": {
...
"react": "^16.5.2",
"react-dom": "^16.4.0",
"react-loadable": "^5.4.0",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.5"
},
"devDependencies": {
"@babel/cli": "^7.1.0",
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^9.0.0"
...
}
My babel.config.js
is the following:我的babel.config.js
如下:
module.exports({
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-flow']
})
I have done as much digging through the internet as I could to try and resolve this problem.我已经通过互联网进行了尽可能多的挖掘,以尝试解决这个问题。 Ended up finding many similar posts to this but, none of the suggested solutions worked out for me.最终找到了许多与此类似的帖子,但是,没有一个建议的解决方案适合我。 I saw one or two that mentioned including the @babel/preset-env
and @babel/preset-react
presets in babel.config.js
which I already have.我在babel.config.js
中看到一两个提到的包括@babel/preset-env
和@babel/preset-react
预设,我已经有了。 Hoping the community has some insight to share.希望社区有一些见解可以分享。
I posted an issue thread on GitHub around this problem and was able to get it resolved by working with two of the Create React App contributors.我在 GitHub 上发布了一个关于这个问题的问题线程,并通过与两个 Create React App 贡献者合作解决了这个问题。 Some great subtle points were brought to attention with respect to how Babel works when using Create React App.在使用 Create React App 时,Babel 的工作方式引起了一些重要的注意。 I definitely recommend giving the conversation a read for those of you suffering from the same problem I did here.我绝对建议您为那些遇到我在这里遇到的同样问题的人阅读对话。
https://github.com/facebook/create-react-app/issues/5135 https://github.com/facebook/create-react-app/issues/5135
There is one thing I will emphasize for those of you using Create React App: Delete all babel related devDependencies and reinstall your node_modules .对于那些使用 Create React App 的人,我要强调一件事:删除所有与 babel 相关的 devDependencies 并重新安装 node_modules 。 Create React App does NOT care or look for .babelrc
or babel.config.js
files in your module. Create React App不关心或查找模块中的.babelrc
或babel.config.js
文件。 They are useless (at least currently, because only time will tell if this behavior is subject to change).它们是无用的(至少目前是这样,因为只有时间才能证明这种行为是否会发生变化)。 If that doesn't work, then additionally delete your package-lock.json
file along with your node_modules
and try again.如果这不起作用,则另外删除package-lock.json
文件和node_modules
试。
The only babel
devDepdendency that I kept in my package (which most likely did not contribute to my problem) was babel-eslint
since I'm using ESLint in my project.我保存在包中的唯一babel
devDepdendency(很可能不会导致我的问题)是babel-eslint
因为我在我的项目中使用 ESLint。
From Babel7 onwards, they expect config to be returned from a function.从 Babel7 开始,他们期望从函数返回配置。 Its better for caching.它更适合缓存。
Now to solve your issue, try this现在要解决您的问题,试试这个
function loadConfig() {
return {
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-flow']
}
}
module.exports = loadConfig;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.