Skip to content

fix(codegen): read react-native.config.cjs so ESM projects are not silently skipped - #58394

Closed
CAMOBAP wants to merge 1 commit into
react:mainfrom
CAMOBAP:codegen-read-react-native-config-cjs
Closed

fix(codegen): read react-native.config.cjs so ESM projects are not silently skipped#58394
CAMOBAP wants to merge 1 commit into
react:mainfrom
CAMOBAP:codegen-read-react-native-config-cjs

Conversation

@CAMOBAP

@CAMOBAP CAMOBAP commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Summary

readReactNativeConfig resolves the config with a single hardcoded extension:

const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');

A project whose package.json declares "type": "module" cannot use that filename. Node parses .js as ESM there, and the require() two lines below either throws or returns the wrong shape:

Config file require() result
react-native.config.js, CommonJS body throws ReferenceError: module is not defined in ES module scope — uncaught here, so codegen dies
react-native.config.js, ESM body returns {__esModule, default}, so rnConfig.dependencies is undefined
react-native.config.cjs returns the config correctly

.cjs is the only extension that is both require()-able and legal in an ESM package — and @react-native-community/cli already treats it as first-class:

// @react-native-community/cli-config/build/readConfigFromDisk.js
const searchPlacesForCJS = ['react-native.config.js', 'react-native.config.cjs', 'react-native.config.ts'];

added for react-native-community/cli#2167. Codegen has its own resolver, does not use cosmiconfig, and was never brought in line. This PR closes that gap.

Why it matters

Autolinking and codegen disagree about the same file, so the failure is silent and deferred. Autolinking reads the .cjs config and installs the pod; codegen doesn't, so it generates nothing for that library; the pod then compiles and its Fabric component includes a header that was never generated:

.../react/renderer/components/RNCSlider/RNCSliderShadowNode.h:5:10:
fatal error: 'react/renderer/components/RNCSlider/Props.h' file not found

Nothing in that error points at the config. findLibrariesFromReactNativeConfig bails via if (!rnConfig.dependencies) return []; without logging.

The other two config sources do not cover it:

  • build/generated/autolinking/autolinking.json is written by use_native_modules! during pod install, so it is absent whenever codegen runs first (pipelines that codegen before installing pods, or generate-codegen-artifacts.js invoked directly).
  • The package.json dependency scan only sees direct dependencies. A library reached through dependencies[...].root — the whole reason to write a config file — is by definition not one.

Changelog:

[General] [Fixed] - Codegen now also reads react-native.config.cjs, so libraries declared in it are no longer skipped in ESM projects

Test Plan

Four cases added to packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js, following the existing mkdtempSync fixture pattern:

yarn jest packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js

  readReactNativeConfig
    ✓ reads react-native.config.js
    ✓ reads react-native.config.cjs when there is no .js config
    ✓ prefers react-native.config.js when both exist
    ✓ returns an empty config when neither exists

Test Suites: 1 passed, 1 total
Tests:       48 passed, 48 total
Snapshots:   24 passed, 24 total

All 48 tests in the file pass, including the 24 existing snapshots — the change is additive for projects that have a .js config.

Negative control: with the utils.js change reverted and the tests left in place, the .cjs case fails as expected:

  ✕ reads react-native.config.cjs when there is no .js config
Tests: 1 failed, 47 skipped, 48 total

Notes

The log line in the else branch now names the directory searched rather than a single path, since there are two candidates:

-    codegenLog(`Could not find React Native config at: ${rnConfigFilePath}`);
+    codegenLog(`Could not find React Native config in: ${projectRoot}`);

Happy to extend this to .mjs / .ts to fully match the CLI's searchPlaces, but neither is a drop-in — .mjs cannot be require()d for the right default-export shape, and .ts needs a loader — so this PR keeps to the one case that is.

@meta-cla

meta-cla Bot commented Sep 8, 2026

Copy link
Copy Markdown

Hi @CAMOBAP!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

…lently skipped

readReactNativeConfig resolves the config with a single hardcoded extension:

  const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');

A project whose package.json declares "type": "module" cannot use that
filename. Node parses .js as ESM there, and the require() two lines below
either throws (CommonJS body -- uncaught here, so codegen dies) or returns
{__esModule, default}, so rnConfig.dependencies is undefined and
findLibrariesFromReactNativeConfig returns [] without logging.

.cjs is the only extension that is both require()-able and legal in an ESM
package, and @react-native-community/cli already treats it as first-class via
searchPlacesForCJS. Codegen has its own resolver, does not use cosmiconfig, and
was never brought in line.

The result is that autolinking and codegen disagree about the same file:
autolinking reads the config and installs the pod, codegen does not and
generates nothing for that library, and the pod then fails to compile because
its Fabric component includes a header that was never generated.

Probe .cjs in addition to .js. .js is checked first, so projects that have one
resolve exactly as before.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@CAMOBAP
CAMOBAP force-pushed the codegen-read-react-native-config-cjs branch from 813fee8 to 847da2a Compare September 8, 2026 08:45
@meta-cla

meta-cla Bot commented Sep 8, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 8, 2026
@meta-cla

meta-cla Bot commented Sep 8, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@CAMOBAP
CAMOBAP marked this pull request as ready for review September 8, 2026 12:27
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 8, 2026
@meta-codesync

meta-codesync Bot commented Sep 8, 2026

Copy link
Copy Markdown

@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D119188806.

@meta-codesync meta-codesync Bot closed this in 583c8f3 Sep 9, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Sep 9, 2026
@meta-codesync

meta-codesync Bot commented Sep 9, 2026

Copy link
Copy Markdown

@vzaidman merged this pull request in 583c8f3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant