Hello Web3Auth team,
I’m running into a consistent crash in my React Native + Expo project when using the @web3auth/react-native-sdk
. It’s possible that I set up my expo environment incorrectly, but I’m pretty sure I set it up correctly. The new URL(…) call within the web3auth util.js is failing. Specifically, the app explodes with:
web3auth util.js
const url = new URL(${signerHost(web3AuthNetwork)}/api/configuration
);
TypeError: Cannot read properties of undefined (reading 'decode')
After some debugging, it looks like there is a collision between two different “URL” implementations. I am attempting to use react-native-url-polyfill for the URL polyfill:
- import
react-native-url-polyfill/auto
– This setsglobal.URL
to a JSI-based or native-backedURL
(which works fine in Expo). - import Named imports in the SDK – The Web3Auth SDK does:
js
import { URL, URLSearchParams } from "react-native-url-polyfill";
which appears to pull in the JavaScript fallback from whatwg-url-without-unicode
. This fallback requires some Node-like APIs (e.g., a working TextDecoder
path), and it doesn’t seem to play nicely in the React Native environment.
My investigation showed that if I remove or comment out the named imports in the SDK code and rely solely on the global URL
(which react-native-url-polyfill/auto
provides), everything starts working again. But, of course, editing node_modules/@web3auth/react-native-sdk/
directly isn’t a great long-term approach.
Here’s a rough outline of my setup and findings:
- Environment:
- Expo SDK 52
- React Native 0.72
- node version v22.12.0
@web3auth/react-native-sdk
8.1.0- Using
react-native-url-polyfill@2.0.0
withauto
at my application entry point
- Symptoms:
Cannot read properties of undefined (reading 'decode')
- Stack trace points to
whatwg-url-without-unicode
code used by Web3Auth
- Workarounds tried:
- Monkey-patching with
global.URL
/global.URLSearchParams
in my ownglobals.js
before loading the rest of the app. - Metro config aliasing to force
whatwg-url-without-unicode
to map toreact-native-url-polyfill
. - Removing named imports in the SDK and relying on the global URL polyfill. (Works, but is a direct node_modules edit.)
- Monkey-patching with
The last option fully resolves the crash, which implies that the fallback polyfill from whatwg-url-without-unicode
is conflicting with the JSI-based URL
in React Native.
Is this behavior by design, or is it a bug on your end?
Would it be possible for the SDK to rely on the globally patched URL
(from react-native-url-polyfill/auto
) or at least provide an option to do so? That would ensure everything uses a single consistent URL implementation in the React Native environment.
I’d love to hear if there’s an official or recommended fix, so we don’t have to patch the SDK manually in our project.
Thanks so much!
globals.js
import “react-native-url-polyfill/auto”;
global.URL = global.URL || require(“react-native-url-polyfill”).URL;
global.URLSearchParams = global.URLSearchParams || require(“react-native-url-polyfill”).URLSearchParams;
import { Buffer } from “buffer”;
global.Buffer = Buffer;
metro.config.js
// Learn more Metro bundler - Expo Documentation
const { getDefaultConfig } = require(“expo/metro-config”);
/** @type {import(‘expo/metro-config’).MetroConfig} */
const config = getDefaultConfig(__dirname);
config.resolver.extraNodeModules = {
assert: require.resolve(“empty-module”), // assert can be polyfilled here if needed
http: require.resolve(“empty-module”), // stream-http can be polyfilled here if needed
https: require.resolve(“empty-module”), // https-browserify can be polyfilled here if needed
os: require.resolve(“empty-module”), // os-browserify can be polyfilled here if needed
url: require.resolve(‘react-native-url-polyfill’), // url can be polyfilled here if needed REPLACED EMPTY-module here/
zlib: require.resolve(“empty-module”), // browserify-zlib can be polyfilled here if needed
path: require.resolve(“empty-module”),
crypto: require.resolve(“crypto-browserify”),
stream: require.resolve(“readable-stream”),
buffer: require.resolve(“buffer”),
};
By the way, I am almost certain this is related to