apollo-client: TypeError: undefined is not a constructor (evaluating 'new _client.InMemoryCache()')

I am trying to use apollo client with React Native as indicated in the documentation at this link: https://www.apollographql.com/docs/react/integrations/react-native/

But i get this error: TypeError: undefined is not a constructor (evaluating โ€˜new _client.InMemoryCache()โ€™)

import React from 'react';
import { AppRegistry } from 'react-native';
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client';

// Initialize Apollo Client
const client = new ApolloClient({
  uri: 'http://192.168.1.168:8080/v1/graphql',
  cache: new InMemoryCache(),
});

const App = () => (
  <ApolloProvider client={client}>
    <MyRootComponent />
  </ApolloProvider>
);

AppRegistry.registerComponent('MyApplication', () => App);

Actual outcome:

TypeError: undefined is not a constructor (evaluating 'new _client.InMemoryCache()')
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:116853:3 in global code

Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

React-native

Versions

System: OS: macOS 11.6 Binaries: Node: 16.13.1 - /usr/local/bin/node Yarn: 1.22.17 - /usr/local/bin/yarn npm: 8.1.2 - /usr/local/bin/npm Browsers: Chrome: 96.0.4664.110 Firefox: 95.0 Safari: 14.1.2 npmPackages: @apollo/client: ^3.5.6 => 3.5.6

Below my package.json

{
  "name": "egregio-avvocato-app",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@apollo/client": "^3.5.6",
    "dotenv": "^10.0.0",
    "expo": "~44.0.0",
    "expo-status-bar": "~1.2.0",
    "graphql": "^16.2.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-web": "0.17.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9"
  },
  "private": true
}

I hope you can help me!

C.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (2 by maintainers)

Most upvoted comments

After a few hours of messing around with the code I realized I needed to modify my metro.config.js file.

Before it Looked Like this:

// Learn more https://docs.expo.io/guides/customizing-metro

const { getDefaultConfig } = require('expo/metro-config');

module.exports = getDefaultConfig(__dirname);

Now it Looks Like this:

// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require('expo/metro-config');

const defaultConfig = getDefaultConfig(__dirname);

defaultConfig.resolver.sourceExts.push('cjs');

module.exports = defaultConfig;

same issue here

i guess @apollo/client@3.5.x have that problem, iโ€™ve downgraded to 3.4.16 and everything is working just fine ๐Ÿ‘

For more details about this issue, please see this notice in the Apollo Client 3.5.4 CHANGELOG. This is a known Metro issue but the workaround mentioned here and in that notice should help for now.

i guess @apollo/client@3.5.x have that problem, iโ€™ve downgraded to 3.4.16 and everything is working just fine ๐Ÿ‘

Downgrading to @apollo/client to v3.4.x was the only thing that worked for me.

None of the other recommended solutions in this issue seems to have worked for me.

@torontochen now I get this back:

TypeError: undefined is not an object (evaluating '_zenObservableTs.Observable.prototype')
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:141487:3 in global code

Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

Did you get the same issue?