RealmJS TypeError: Cannot read property 'Realm' of undefined
RealmJS TypeError: Cannot read property 'Realm' of undefined
这是我在使用 React Native 应用程序时遇到的 RealmJS 问题。我花了四天多的时间试图调试出了什么问题(我已经构建了应用程序的重要部分,认为它会在较新的版本中得到修补)。希望这对某人有所帮助。
我正在尝试调试我的 React Native 应用程序,但由于 RealmJS 而无法调试。当我 运行 我的模拟器或设备(iOS 或 Android)时,该应用程序运行良好。但是,打开远程调试,应用程序只会在启动时挂起。我收到以下错误以及空白屏幕:
TypeError: Cannot read property 'Realm' of undefined
reactConsoleErrorHandler @ ExceptionsManager.js:179
n @ backend.js:32
reportException @ ExceptionsManager.js:104
handleException @ ExceptionsManager.js:171
handleError @ setUpErrorHandling.js:24
reportFatalError @ error-guard.js:49
guardedLoadModule @ require.js:204
metroRequire @ require.js:128
(anonymous) @ index.js:118
executeApplicationScript @ RNDebuggerWorker.js:2
(anonymous) @ RNDebuggerWorker.js:2
我试过重置缓存(watchman、react-native、pods 等)、清理构建文件夹,甚至重启我的机器,但似乎没有任何效果。在一个新项目中安装 Realm 表明它工作得很好,但我不想重做我的整个项目。我做错了什么?
设置(npx react-native info
):
System:
OS: macOS 10.15.5
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 230.89 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.12.0 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.8 - ~/Projects/MyApp/node_modules/.bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK: Not Found
IDEs:
Android Studio: 4.0 AI-193.6911.18.40.6626763
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 14.0.2 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: ^4.13.0 => 4.13.0
react: 16.13.1 => 16.13.1
react-native: 0.63.3 => 0.63.3
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
我花了很多时间试图解决这个问题。将我的项目拼凑起来后 bit-by-bit,我发现了问题:babel-plugin-module-resolver
。
为了创建自定义路径别名(即 "@components/" -> "./src/components/"
),我必须安装 babel-plugin-module-resolver
作为开发依赖项。
当我像这样设置插件时出现了问题:
// babel.config.js
module.exports = {
presets: ["module:metro-react-native-babel-preset"],
plugins: [
[
"module-resolver",
{
root: ["."],
cwd: "packagejson",
extensions: [
".ts",
".tsx",
".js",
".jsx",
".ios.js",
".android.js",
".ios.ts",
".android.ts",
".ios.tsx",
".android.tsx",
],
alias: {
"@components": "./src/components",
},
},
],
],
};
解决方案是将 cwd
从 packagejson
更改为 babelrc
。
// babel.config.js
module.exports = {
presets: ["module:metro-react-native-babel-preset"],
plugins: [
[
"module-resolver",
{
...
cwd: "babelrc", // <-- here
...
在那之后,我可以 运行 正确调试!
Note: At the time of this answer there is another error that appears with the debugger for Realm 10.0.1 and RN 0.63.3, but this is an actual bug as opposed to a user error.
这是我在使用 React Native 应用程序时遇到的 RealmJS 问题。我花了四天多的时间试图调试出了什么问题(我已经构建了应用程序的重要部分,认为它会在较新的版本中得到修补)。希望这对某人有所帮助。
我正在尝试调试我的 React Native 应用程序,但由于 RealmJS 而无法调试。当我 运行 我的模拟器或设备(iOS 或 Android)时,该应用程序运行良好。但是,打开远程调试,应用程序只会在启动时挂起。我收到以下错误以及空白屏幕:
TypeError: Cannot read property 'Realm' of undefined
reactConsoleErrorHandler @ ExceptionsManager.js:179
n @ backend.js:32
reportException @ ExceptionsManager.js:104
handleException @ ExceptionsManager.js:171
handleError @ setUpErrorHandling.js:24
reportFatalError @ error-guard.js:49
guardedLoadModule @ require.js:204
metroRequire @ require.js:128
(anonymous) @ index.js:118
executeApplicationScript @ RNDebuggerWorker.js:2
(anonymous) @ RNDebuggerWorker.js:2
我试过重置缓存(watchman、react-native、pods 等)、清理构建文件夹,甚至重启我的机器,但似乎没有任何效果。在一个新项目中安装 Realm 表明它工作得很好,但我不想重做我的整个项目。我做错了什么?
设置(npx react-native info
):
System:
OS: macOS 10.15.5
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 230.89 MB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 14.12.0 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.14.8 - ~/Projects/MyApp/node_modules/.bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
Managers:
CocoaPods: 1.9.3 - /usr/local/bin/pod
SDKs:
iOS SDK:
Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
Android SDK: Not Found
IDEs:
Android Studio: 4.0 AI-193.6911.18.40.6626763
Xcode: 11.6/11E708 - /usr/bin/xcodebuild
Languages:
Java: 14.0.2 - /usr/bin/javac
Python: 2.7.16 - /usr/bin/python
npmPackages:
@react-native-community/cli: ^4.13.0 => 4.13.0
react: 16.13.1 => 16.13.1
react-native: 0.63.3 => 0.63.3
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
我花了很多时间试图解决这个问题。将我的项目拼凑起来后 bit-by-bit,我发现了问题:babel-plugin-module-resolver
。
为了创建自定义路径别名(即 "@components/" -> "./src/components/"
),我必须安装 babel-plugin-module-resolver
作为开发依赖项。
当我像这样设置插件时出现了问题:
// babel.config.js
module.exports = {
presets: ["module:metro-react-native-babel-preset"],
plugins: [
[
"module-resolver",
{
root: ["."],
cwd: "packagejson",
extensions: [
".ts",
".tsx",
".js",
".jsx",
".ios.js",
".android.js",
".ios.ts",
".android.ts",
".ios.tsx",
".android.tsx",
],
alias: {
"@components": "./src/components",
},
},
],
],
};
解决方案是将 cwd
从 packagejson
更改为 babelrc
。
// babel.config.js
module.exports = {
presets: ["module:metro-react-native-babel-preset"],
plugins: [
[
"module-resolver",
{
...
cwd: "babelrc", // <-- here
...
在那之后,我可以 运行 正确调试!
Note: At the time of this answer there is another error that appears with the debugger for Realm 10.0.1 and RN 0.63.3, but this is an actual bug as opposed to a user error.