java.lang.RuntimeException: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined
Description
To improve the performance of my React Native app, I modularized the app code into separate bundles, including a 'common' and an 'index' bundle. However, starting from version 0.73.6, when my Android app loads the 'common' bundle, the following error occurs:(But I found 0.72.5 don't has this question.) [email protected] [email protected]
FATAL EXCEPTION: mqt_js (Ask Gemini)
Process: com.splitrn_0736, PID: 23330
java.lang.RuntimeException: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined
at com.facebook.react.bridge.DefaultJSExceptionHandler.handleException(DefaultJSExceptionHandler.java:20)
at com.facebook.react.devsupport.DisabledDevSupportManager.handleException(DisabledDevSupportManager.java:195)
at com.facebook.react.bridge.CatalystInstanceImpl.onNativeException(CatalystInstanceImpl.java:614)
at com.facebook.react.bridge.CatalystInstanceImpl.-$$Nest$monNativeException(Unknown Source:0)
at com.facebook.react.bridge.CatalystInstanceImpl$NativeExceptionHandler.handleException(CatalystInstanceImpl.java:632)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:40)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
at java.lang.Thread.run(Thread.java:1012)
Caused by: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined
at com.facebook.jni.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
at java.lang.Thread.run(Thread.java:1012)
Caused by: com.facebook.jni.CppException: Cannot read property 'setGlobalHandler' of undefined
TypeError: Cannot read property 'setGlobalHandler' of undefined
at anonymous (common.android.bundle:157:150)
at h (common.android.bundle:2:1789)
at d (common.android.bundle:2:1250)
at i (common.android.bundle:2:501)
at anonymous (common.android.bundle:143:85)
at h (common.android.bundle:2:1789)
at d (common.android.bundle:2:1250)
at i (common.android.bundle:2:501)
at global (common.android.bundle:484:4)
... 8 more
Steps to reproduce
- yarn
- npm run build:common
- npm run build:index
- run android app in android studio
React Native Version
0.73.6
Affected Platforms
Runtime - Android
Output of npx react-native info
System:
OS: macOS 14.4.1
CPU: (11) arm64 Apple M3 Pro
Memory: 187.81 MB / 18.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 18.18.2
path: ~/.nvm/versions/node/v18.18.2/bin/node
Yarn:
version: 1.22.19
path: ~/.nvm/versions/node/v18.18.2/bin/yarn
npm:
version: 9.8.1
path: ~/.nvm/versions/node/v18.18.2/bin/npm
Watchman:
version: 2024.01.22.00
path: /opt/homebrew/bin/watchman
Managers:
CocoaPods:
version: 1.15.0
path: /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 23.2
- iOS 17.2
- macOS 14.2
- tvOS 17.2
- watchOS 10.2
Android SDK: Not Found
IDEs:
Android Studio: 2023.3 AI-233.14808.21.2331.11709847
Xcode:
version: 15.1/15C65
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.9
path: /Users/haoyiwen/Library/Java/JavaVirtualMachines/corretto-17.0.9/Contents/Home/bin/javac
Ruby:
version: 2.6.10
path: /usr/bin/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.2.0
wanted: 18.2.0
react-native:
installed: 0.73.6
wanted: 0.73.6
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: true
newArchEnabled: false
iOS:
hermesEnabled: Not found
newArchEnabled: false
Stacktrace or Logs
FATAL EXCEPTION: mqt_js (Ask Gemini)
Process: com.splitrn_0736, PID: 23330
java.lang.RuntimeException: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined
at com.facebook.react.bridge.DefaultJSExceptionHandler.handleException(DefaultJSExceptionHandler.java:20)
at com.facebook.react.devsupport.DisabledDevSupportManager.handleException(DisabledDevSupportManager.java:195)
at com.facebook.react.bridge.CatalystInstanceImpl.onNativeException(CatalystInstanceImpl.java:614)
at com.facebook.react.bridge.CatalystInstanceImpl.-$$Nest$monNativeException(Unknown Source:0)
at com.facebook.react.bridge.CatalystInstanceImpl$NativeExceptionHandler.handleException(CatalystInstanceImpl.java:632)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:40)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
at java.lang.Thread.run(Thread.java:1012)
Caused by: com.facebook.react.devsupport.JSException: Cannot read property 'setGlobalHandler' of undefined
at com.facebook.jni.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
at android.os.Looper.loopOnce(Looper.java:222)
at android.os.Looper.loop(Looper.java:314)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:234)
at java.lang.Thread.run(Thread.java:1012)
Caused by: com.facebook.jni.CppException: Cannot read property 'setGlobalHandler' of undefined
TypeError: Cannot read property 'setGlobalHandler' of undefined
at anonymous (common.android.bundle:157:150)
at h (common.android.bundle:2:1789)
at d (common.android.bundle:2:1250)
at i (common.android.bundle:2:501)
at anonymous (common.android.bundle:143:85)
at h (common.android.bundle:2:1789)
at d (common.android.bundle:2:1250)
at i (common.android.bundle:2:501)
at global (common.android.bundle:484:4)
... 8 more
Reproducer
https://github.com/Hao-yiwen/reactNative-study/tree/master/splitRn_0736
Screenshots and Videos
| :warning: | Newer Version of React Native is Available! |
|---|---|
| :information_source: | You are on a supported minor version, but it looks like there's a newer patch available - 0.73.8. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases. |
0.74.1 also have this question.
import 'react';
import 'react-native';
if (!__DEV__) {
console = {
info: () => {},
log: () => {},
warn: () => {},
error: () => {},
time: () => {},
timeEnd: () => {},
};
}
This is my common.js.
My app first load common.jsbundle,then load index.android.bundle.Question happen in first load.
I found It's my problem.Chnage metro.config.js to follow is ok.
function createModuleIdFactory() {
console.log('init common -------->');
const fileToIdMap = new Map();
let nextId = 0;
clean("./config/bundleCommonInfo.json");
// 如果是业务 模块请以 10000000 来自增命名
return (path) => {
let id = fileToIdMap.get(path);
if (typeof id !== "number") {
id = nextId++;
fileToIdMap.set(path, id);
!hasBuildInfo("./config/bundleCommonInfo.json", path) &&
writeBuildInfo(
"./config/bundleCommonInfo.json",
path,
fileToIdMap.get(path)
);
}
return id;
};
}
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
serializer: {
createModuleIdFactory: createModuleIdFactory, // 给 bundle 一个id 避免冲突 cli 源码中这个id 是从1 开始 自增的
},
};
module.exports = mergeConfig(getDefaultConfig(__dirname), config);
@Hao-yiwen could you explain what change you actually made to your metro config? I am also facing this issue
I get this question,because I change the metro default config on my react-native project.I solve this question by set default metro config with @react-native/metro-config.I checked this problem before because the default metro config is not set.
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
serializer: {
createModuleIdFactory: createModuleIdFactory, // 给 bundle 一个id 避免冲突 cli 源码中这个id 是从1 开始 自增的
},
};
module.exports = mergeConfig(getDefaultConfig(__dirname), config);