迁移到 React Native 0.49

Migrating to react native 0.49

最近我尝试将 React Native 版本从 0.48.4 更新到 0.49.0。我已阅读 documentation 关于破坏 changes.I 安装的 react-native v0.49.0。当我尝试 运行 时出现这样的错误

node_modules\react-native\local-cli\util\findSymlinkedModules.js:37
ignoredRoots? = [])
            ^
SyntaxError: Unexpected token ?
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at loader (\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)

我尝试使用 react-native-git-upgradereact-native upgrade 进行升级。但是出现了诸如无法识别为内部或外部命令之类的错误。

我在 RN 0.49.0 的新功能和增强更改日志中发现了一个这样的更改

Refactor how symlinks are discovered in local-cli, support scoped modules.

有什么关系吗

如果您使用的是 git 存储库,请尝试删除项目文件夹,然后重新克隆存储库。这对遇到同样问题的朋友有效。

我在这里找到了这个解决方案: https://github.com/facebook/react-native/issues/16458

为了解决这个问题,我不得不使用 babel-plugin-transform-flow-strip-types 插件。

步骤:

npm install --save-dev babel-plugin-transform-flow-strip-types

.babelrc

{
  "plugins": ["transform-flow-strip-types"]
}

更多详情https://www.npmjs.com/package/babel-plugin-transform-flow-strip-types

升级到 "react-native": "0.54.1", 为我解决了这个问题。

运行 更新到 0.54 时遇到同样的问题。

这里的根本原因是 Node 试图用 Flow 类型解释 JS 代码(在本例中,ignoredRoots?)。

通常 Babel 会按照 Dinesh 的建议通过 babel-plugin-transform-flow-strip-types 删除这些类型。但是这个转换包含在标准的 react-native Babel 预设中,它也做了很多其他的事情。

因此,您真正需要做的是确保您拥有该预设设置。它应该在 package.json 中的 devDependency 下:

"devDependencies": {
  "babel-preset-react-native": "^4.0.0",

然后在.babelrc中配置:

{
  "presets": ["react-native"]
}

设置完成后,您可能仍需要重置打包程序缓存:

npm start -- --reset-cache

这就是为我做的,希望对您有所帮助。

findSymlinkedModules.js 第 37 行删除 ? 对我有用