title: '[iOS] Runtime not ready: Invariant Violation with NativeEventEmitter in React Native'
labels: ['bug', 'ios', 'needs-triage']
Description
We're encountering an Invariant Violation error when trying to create a NativeEventEmitter in our React Native iOS app. The error suggests that the runtime is not ready when attempting to create the event emitter. This issue appears to be iOS-specific and is related to the timing of native module initialization. The error doesn't occur on Android.
Environment
- React Native Version: 0.77.0
- React Version: 18.3.1
- iOS Version: 18.2 (SDK)
- Device: iOS Simulator/Physical Device
- Development Environment: macOS 15.2
- Node Version: 20.18.1
- Xcode Version: 16.2/16C5032a
- CocoaPods Version: 1.16.2
- New Architecture: Enabled (newArchEnabled: true)
- Hermes: Disabled on iOS, Enabled on Android
Steps to Reproduce
- Start a React Native app on iOS
- Try to create a NativeEventEmitter for any native module
- The app crashes with the following error:
[runtime not ready]: Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
invariant
&platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.automind.v4:4931:25
NativeEventEmitter
&platform=ios&dev=true&lazy=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.automind.v4:37118:31
Current Behavior
The app crashes on iOS with a runtime not ready error when trying to initialize any NativeEventEmitter. The error suggests that the JavaScript runtime is not ready when the event emitter is being created. This is particularly problematic with the New Architecture enabled.
Expected Behavior
The NativeEventEmitter initialization should either:
- Wait for the runtime to be ready before allowing event emitter creation
- Handle the case where the runtime isn't ready yet gracefully
- Provide a clear way to check if the runtime is ready before creating event emitters
Attempted Solutions
We've tried several approaches to work around this issue:
- Lazy initialization of the event emitter
- Checking for module existence before creating the emitter
- Adding error handling around emitter creation
- Using Platform-specific code for iOS
- Enabling/Disabling Hermes on iOS (currently disabled)
However, the issue persists, suggesting this might be a deeper issue with how the React Native runtime initializes on iOS, particularly with the New Architecture enabled.
Code Example
Here's a minimal reproduction of the issue:
import {NativeEventEmitter, NativeModules} from 'react-native';
// This line causes the error on iOS during app startup
const eventEmitter = new NativeEventEmitter(NativeModules.SomeModule);
Even with defensive programming:
const getEmitter = () => {
if (!NativeModules.SomeModule) {
console.warn('Native module not available');
return null;
}
try {
return new NativeEventEmitter(NativeModules.SomeModule);
} catch (error) {
console.error('Failed to create event emitter:', error);
return null;
}
};
Additional Context
- This issue only occurs on iOS
- The error happens during the initial load of the app
- Android works as expected with the same code
- The issue seems related to the timing of runtime initialization
- We're using the New Architecture in React Native (newArchEnabled: true)
- The issue persists regardless of Hermes being enabled or disabled on iOS
- All development tools and SDKs are up to date:
- Watchman: 2024.12.02.00
- CocoaPods: 1.16.2
- Node: 20.18.1
- npm: 10.8.2
Questions
- Is there a recommended way to handle runtime initialization timing on iOS with the New Architecture?
- Should there be a way to check if the runtime is ready before creating an event emitter?
- Is this a known issue with the New Architecture and iOS?
- Are there any workarounds or best practices for handling event emitter creation during app startup with the New Architecture?
- Could this be related to the Hermes configuration difference between iOS and Android?
Relevant Information
title: '[iOS] Runtime not ready: Invariant Violation with NativeEventEmitter in React Native'
labels: ['bug', 'ios', 'needs-triage']
Description
We're encountering an
Invariant Violationerror when trying to create aNativeEventEmitterin our React Native iOS app. The error suggests that the runtime is not ready when attempting to create the event emitter. This issue appears to be iOS-specific and is related to the timing of native module initialization. The error doesn't occur on Android.Environment
Steps to Reproduce
Current Behavior
The app crashes on iOS with a runtime not ready error when trying to initialize any NativeEventEmitter. The error suggests that the JavaScript runtime is not ready when the event emitter is being created. This is particularly problematic with the New Architecture enabled.
Expected Behavior
The NativeEventEmitter initialization should either:
Attempted Solutions
We've tried several approaches to work around this issue:
However, the issue persists, suggesting this might be a deeper issue with how the React Native runtime initializes on iOS, particularly with the New Architecture enabled.
Code Example
Here's a minimal reproduction of the issue:
Even with defensive programming:
Additional Context
Questions
Relevant Information