Skip to content

fix: don't crash at import time when native modules are unavailable - #462

Open
AhmedAlAmawi wants to merge 2 commits into
intercom:mainfrom
AhmedAlAmawi:fix/guard-missing-native-modules
Open

fix: don't crash at import time when native modules are unavailable#462
AhmedAlAmawi wants to merge 2 commits into
intercom:mainfrom
AhmedAlAmawi:fix/guard-missing-native-modules

Conversation

@AhmedAlAmawi

@AhmedAlAmawi AhmedAlAmawi commented Aug 2, 2026

Copy link
Copy Markdown

Description

When the Intercom native module isn't available — Expo Go, Jest, a web bundle, or a prebuild before pods are installed — NativeModules.IntercomEventEmitter is undefined. Since src/index.tsx reads constants off it at module scope (IntercomEventEmitter.UNREAD_COUNT_CHANGE_NOTIFICATION, WINDOW_DID_SHOW_NOTIFICATION, …), merely importing the package throws:

TypeError: Cannot read property 'UNREAD_COUNT_CHANGE_NOTIFICATION' of undefined

and crashes the app at boot, before any Intercom API is called.

This PR falls back to an empty object (?? {}) for both IntercomModule and IntercomEventEmitter, deferring the failure from import time to the first actual API call — where callers can handle it. Behavior is completely unchanged whenever the native module exists.

We've been running this exact change as a local patch in our production React Native (Expo) app — it's what lets the package coexist with environments where the native side isn't present.

Changes

  • src/nativeModules.ts: ?? {} fallback on both exports
  • __tests__/nativeModules.test.ts: covers both paths — exports fall back to empty objects when the native module is missing (import doesn't throw), and pass through untouched when it's registered

Verification

  • yarn typescript
  • yarn lint
  • yarn jest __tests__/nativeModules.test.ts ✅ (2/2 passing)

Notes

  • CONTRIBUTING.md says to target dev, but that branch's last commit is from 2022 and all recently merged PRs target main, so this targets main. Happy to retarget if dev is still the intended base.
  • CONTRIBUTING.md also asks for an issue first for implementation changes, but issues are disabled on this repo — so the discussion lives here.

When the native module isn't linked (Expo Go, Jest, web bundles, a
prebuild before pods are installed), NativeModules.IntercomEventEmitter
is undefined and the module-scope constant reads in index.tsx
(e.g. IntercomEventEmitter.UNREAD_COUNT_CHANGE_NOTIFICATION) throw at
import time, crashing the app on boot. Fall back to an empty object so
failure is deferred to actual API calls instead of the import.
@AhmedAlAmawi
AhmedAlAmawi force-pushed the fix/guard-missing-native-modules branch from 1d0704e to 1c9a7c4 Compare August 2, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant