@react-native/[email protected] assigns module.exports inside an ES module, so non-Metro web bundles crash
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, react-native, webpack
Research direction
The issue identifies @react-native/assets-registry/registry.js as the failing file and gives the replacement named exports. Read that file and check its package declarations; then run a webpack 5 or Rspack web bundle that imports react-native-svg with react-native aliased to react-native-web. Done when the bundle loads without the ES module error and the named imports resolve.
Written by the indexing model from the issue text.
Description
Description
@react-native/[email protected] registry.js contains an import and then assigns module.exports. Rspack and webpack 5 treat a file with import as an ES module and throw when it assigns module.exports. The web bundle never evaluates past that file.
react-native-svg imports the named export, so any web bundle that includes it hits this on startup:
import { getAssetByID } from "@react-native/assets-registry/registry";
Metro accepts the mix, so native builds and Metro web builds do not throw.
This is not #48349. That issue was Flow syntax (export type) in the 0.76 file, fixed by #48458, which moved the types into comments and kept module.exports. The 0.88 file is a later rewrite: it imports AssetRegistry from react-native and still assigns module.exports. The published registry.d.ts already declares registerAsset and getAssetByID as named exports.
Published registry.js:
"use strict";
import { AssetRegistry } from "react-native";
/*::
export type {AssetDestPathResolver, PackagerAsset} from 'react-native';
*/
// eslint-disable-next-line @react-native/monorepo/no-commonjs-exports
module.exports = {
registerAsset: AssetRegistry.registerAsset,
getAssetByID: AssetRegistry.getAssetByID,
};
Replacing the assignment with named exports matches the .d.ts and lets the bundle load:
export const registerAsset = AssetRegistry.registerAsset;
export const getAssetByID = AssetRegistry.getAssetByID;
Steps to reproduce
- Install
[email protected],react-native-web, and[email protected]. - Bundle a web entry that imports
react-native-svgwith Rspack or webpack 5, aliasingreact-nativetoreact-native-web. - Load the bundle in a browser.
React Native Version
0.88.1
Affected Platforms
Runtime - Web
Output of npx @react-native-community/cli info
Not a stock React Native app. The failure is in the published @react-native/[email protected] package, loaded through [email protected] by an Rspack web build. react-native is 0.88.1 and react-native-web is 0.21.4.
Stacktrace or Logs
Uncaught (in promise) Error: ES Modules may not assign module.exports or exports.*, Use ESM export syntax, instead: .../node_modules/@react-native/assets-registry/registry.js
Rspack also reports, before the throw:
ESModulesLinkingWarning: export 'getAssetByID' (imported as 'getAssetByID') was not found in '@react-native/assets-registry/registry' (module has no exports)
from react-native-svg/lib/module/lib/resolveAssetUri.js.
Reproducer
The three accepted reproducer forms all bundle with Metro, and Metro does not throw on this file:
- A pull request that edits
RNTesterPlayground.jsruns inside RNTester, which uses Metro. - An Expo Snack uses Metro.
- The reproducer template is a Community CLI app, which also uses Metro.
The crash happens only when Rspack or webpack 5 bundles @react-native/assets-registry/registry.js. A project that reproduces it is a small web build that installs [email protected], react-native-web, and [email protected], aliases react-native to react-native-web, and bundles an entry that imports react-native-svg. Loading that bundle throws ES Modules may not assign module.exports from registry.js.
There is no public repository for that build yet.
- Dominant language
- C++
- Stars
- 127k
- Forks
- 25.5k
- Avg merge
- 3d 19h
- Merged PRs (30d)
- 291
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from react/react-native
-
Needs: Triage :mag: Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
react/react-native#60167 ·
Maintainers usually reply within 2 days
-
Needs: Triage :mag:
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
react/react-native#60000 ·
Maintainers usually reply within 2 days
-
`yarn test-ios` exits before building RNTester: `objc-test.sh` launches the WebSocket server script #59433 deletedPossibly taken @lazerg claimed this 7 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
react/react-native#59939 ·
Maintainers usually reply within 2 days
-
iOS Fabric pointer-handler reset leaves the next lone touch non-primaryPossibly taken @alleneubank claimed this 9 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
react/react-native#59919 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
react/react-native#59722 ·
Maintainers usually reply within 2 days
All issues in react/react-native
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
protocolbuffers/protobuf#31768 ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 2 days
-
tests: ofx_render_test doesn't compile against FFmpeg 4.x (const AVCodec* to av_find_best_stream)Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/openfx-io#221 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
subsurface/subsurface#5145 ·
Maintainers usually reply within 2 days