algoliasearch-client-javascript
algoliasearch-client-javascript copied to clipboard
[bug]: import_http.default.Agent is not a constructor on expo project
Description
I get the following error on web when importing algoliasearch client :
Client
Search
Environment
expo-env-info 1.2.2 environment info:
System:
OS: Linux 5.4 Ubuntu 20.04.6 LTS (Focal Fossa)
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 21.1.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.3.0 - /usr/local/bin/npm
SDKs:
Android SDK:
API Levels: 29, 30, 31, 32, 33, 34
Build Tools: 29.0.2, 30.0.3, 31.0.0, 33.0.0, 33.0.1, 34.0.0, 35.0.0
System Images: android-30 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom_64, android-30 | Google Play Intel x86 Atom
npmPackages:
expo: ~52.0.37 => 52.0.37
expo-router: ~4.0.17 => 4.0.17
react: 18.3.1 => 18.3.1
react-dom: 18.3.1 => 18.3.1
react-native: 0.76.7 => 0.76.7
react-native-web: ^0.19.13 => 0.19.13
npmGlobalPackages:
eas-cli: 7.2.0
expo-cli: 6.3.10
Expo Workflow: managed
Minimal reproducible example : https://github.com/LamsaLL/test-algolia
Not sure of the cause for resolving to the node version on expo, but you can make sure to use the fetch (@algolia/requester-fetch) or the xhr requester (@algolia/requester-browser-xhr)
I think metro resolves to the react-native field (https://github.com/algolia/algoliasearch-client-javascript/blob/main/packages/algoliasearch/package.json#L65) but you might be able to better configure it with https://metrobundler.dev/docs/package-exports/
@Haroenv I tried using requester-fetch and requester-browser-xhr :
import { createFetchRequester } from "@algolia/requester-fetch";
const searchClient = algoliasearch("undefined", "undefined", {
requester: createFetchRequester(),
});
But i get same error. @shortcuts I don't really understand what i need to configure on metro.
I tried using algoliasearch with expo 51.0.39, and it worked well. It seems to be broken only on expo 52.
I also got the same error after upgrading to Expo 52
I resolved the error by downgrading react-native to version 0.74.5. I think the issue is related to react-native 0.76.x. I'm not sure how to make it work with react-native 0.76.x.
setting
config.resolver.unstable_enablePackageExports = true
in metro.config.js fixes the issue for me, tested on "expo": "~52.0.38" with "react-native": "0.76.7"
Try using config.resolver.unstable_enablePackageExports = true with the same versions as you, but it's not working on my side. I'm getting weird errors.
This configuration is also an experimental feature and not yet stabilized.
I only ran into this issue when bundling for web. My solution was to create a stub for web which imported the algolia lite created and added the missing methods that were being consumed from search client used my native app.
https://gist.github.com/christopherdro/eba9cfaf237bee91fcb07bf69c5853e1
I ran into the same issue on Expo Web (import_http.default.Agent is not a constructor) — it happens because Metro resolves the Node version of Algolia instead of the browser one.
✅ Fix: add this to your package.json to force the browser build:
"browser": {
"algoliasearch": "algoliasearch/dist/browser",
"algoliasearch/lite": "algoliasearch/dist/lite/builds/browser"
}
Then restart Metro with cache clear:
npx expo start --clear
This prevents the Node HTTP requester from being loaded and works without any code changes or extra config.
@pierrecabriere's suggestion worked perfectly for me! Thank you