Skip to content

Dynamic import() of a Node-API addon doesn't work on Android #416

Description

@kraenhansen

Tracking the TODO in apps/test-app/App.tsx.

The ferric-example test has to use require() — with an ESLint disable for @typescript-eslint/no-require-imports — because a dynamic import() of the same addon does not work on Android:

it("exports a callable sum function", () => {
const exampleAddon =
/* eslint-disable-next-line @typescript-eslint/no-require-imports -- TODO: Determine why a dynamic import doesn't work on Android */
require("@react-native-node-api/ferric-example") as typeof import("@react-native-node-api/ferric-example");
const result = exampleAddon.sum(1, 3);
if (result !== 4) {

Nobody has established why. It matters beyond the test app: consumers writing modern ESM will reach for await import("some-addon") first, and if that silently fails on Android while working on iOS, it is a confusing first-run experience for exactly the audience this project targets.

Worth checking, roughly in order of likelihood:

  • Whether the Babel plugin in packages/host/src/node/babel-plugin/plugin.ts rewrites require("./addon.node") but leaves import("./addon.node") untransformed — the plugin matches require call expressions, so a dynamic import may simply never be turned into a requireNodeAddon() call.
  • Whether Metro's async-import handling on Android (inline requires / RAM bundles) defers the module in a way that breaks the TurboModule lookup.
  • Whether the failure is a rejected promise or a hard error, and what it actually says — the current comment records the symptom without the message.

Outcome should be either a fix (most likely: handle import() in the Babel plugin alongside require()) or a documented limitation, so the eslint-disable can carry a real explanation instead of an open question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Android 🤖Anything related to the Android platform (Gradle, NDK, Android SDK)Host 🏡Our `react-native-node-api-modules` package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions