使用 babel-preset-react-native/index.js 的 TransformError

使用 babel-preset-react-native/index.js 的 TransformError

问题描述:

我是 react-native 的新手,正在尝试在屏幕上呈现我的第一个文本.

I'm brand new to react-native, and trying to render my first bit of text on the screen.

当我运行 react-native run-ios 然后点击进入我的示例应用程序时,我看到以下错误:

When I run react-native run-ios and then click into my sample app, I see the following error:

TransformError ENOENT: 没有这样的文件或目录,打开'.../albums/node_modules/babel-preset-react-native/index.js' 同时处理预设 '.../babel-preset-react-native/index.js' RCTFatal RCTCxxBridge handleError

index.js 确实存在于我的项目中,代码如下:

index.js DOES exist in my project, with the following code:

'use strict';
module.exports = require('./configs/main');

到目前为止,我已经尝试了以下方法:

So far, I have tried the following:

1) 为 babel-preset-react-native 重新安装 npm

1) Re-installing npm for babel-preset-react-native

2) 使用 yarn 删除然后重新添加 babel-preset-react-native
yarn 移除 babel-preset-react-native
yarn 添加 babel-preset-react-native@2.1.0

2) Using yarn to remove and then re-add babel-preset-react-native
yarn remove babel-preset-react-native
yarn add babel-preset-react-native@2.1.0

3) 彻底删除 babel.rc

3) Deleting babel.rc entirely

没有任何效果.渴望听取有关其他尝试的建议和建议.

Nothing has worked. Eager to hear advice and suggestions for other things to try.

这是 babel-preset-react-native 新版本中的 bug,因为它会在你运行 后自动添加react-native init 命令

This is bug in the new version of babel-preset-react-native as it will added automatically after you run react-native init command

请看一下这个 github 问题 https://github.com/facebook/react-native/issues/15513

Please take a look at this github issue https://github.com/facebook/react-native/issues/15513

解决方法,尝试更改版本:

Workaround, try to change the version:

yarn remove babel-preset-react-native
yarn add babel-preset-react-native@2.1.0

如果这对您不起作用,请尝试在下面的代码中删除 node_modules/babel-preset-react-native/configs/main.js 中的尾随逗号

if that's not work for you, try to remove trailing comma at node_modules/babel-preset-react-native/configs/main.js at this code below

https://github.com/facebook/react-native/blob/6ad7e8281b37ee2ce6425363c0b17420d056807a/babel-preset/configs/main.js#L40并重新运行您的应用