Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: While trying to resolve module @apollo/client React Native #9194

Closed
dragoshuniq opened this issue Dec 13, 2021 · 16 comments
Closed

Error: While trying to resolve module @apollo/client React Native #9194

dragoshuniq opened this issue Dec 13, 2021 · 16 comments

Comments

@dragoshuniq
Copy link

after installing new version of apollo client getting this Error. I tried other versions and to downgrade but nothing. Also I tried to specify in metro.config.js to resolve "cjs" type of file (@apollo/client/main.cjs), but nothing.
Error
error: Error: While trying to resolve module @apollo/client from file ****\src\api\queries\home.js, the package ****\node_modules\@apollo\client\package.json was successfully found. However, this package itself specifies a main module field that could not be resolved (****\node_modules\@apollo\client\main.cjs. Indeed, none of these files exist:

package.json
"graphql": "^15.4.0", "@apollo/client": "^3.3.2",

@AustinHunt
Copy link

Same issue here. Updated from v 3.

@benjamn
Copy link
Member

benjamn commented Dec 13, 2021

@dragoshuniq @AustinHunt Please see the notice about reconfiguring Metro (to enable the .cjs file extension) in the CHANGELOG.md section for Apollo Client v3.5.4. More details in #9084 if you're curious.

@dragoshuniq
Copy link
Author

@benjamn I already tried this solution.

But getting something like this
TypeError: undefined is not an object (evaluating 'InnerNativeModule.installCoreFunctions') ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.

@AustinHunt
Copy link

@benjamn solved this by following those instructions. Thanks.

@benjamn
Copy link
Member

benjamn commented Dec 15, 2021

@dragoshuniq That looks like a different problem (no mention of @apollo/client), so I take it the original problem is fixed?

If the other problem turns out to be related to Apollo Client, please do open a follow-up issue!

@JClackett
Copy link

@benjamn I'm also getting @dragoshuniq's error, I've updated the metro config so it correctly uses cjs, but then all imports from @apollo/client return undefined. Downgrading to a 3.4.x version works, so its definitely an issue with Apollo Client.

Though I do get the same error as @dragoshuniq, before that I get this:

TypeError: undefined is not a function (near '...(0, Apollo.gql)...')

I believe the "Invariant Violation: "main" has not been registered." error happens just because nothing is rendered.

@JClackett
Copy link

I have a repro here as part of my teams boilerplate, its part of a monorepo in packages/app.

@JClackett
Copy link

Nevermind, I was adding cjs to the assetExts instead of sourceExts...

Adding this to metro.config works

config.resolver.sourceExts.push("cjs")

@pruge
Copy link

pruge commented Dec 30, 2021

I am using Recoil. After modifying metro.config.js, the following error occurs.

error: Error: Unable to resolve module react-dom from /Users/pruge/..../node_modules/recoil/cjs/recoil.js: 
react-dom could not be found within the project or in these directories:
  node_modules
  ../../../../node_modules

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
   6 |
   7 | var react = _interopDefault(require('react'));
>  8 | var reactDom = _interopDefault(require('react-dom'));
     |                                         ^
   9 |
  10 | /**
  11 |  * Copyright (c) Facebook, Inc. and its affiliates.

@zenato
Copy link

zenato commented Jan 5, 2022

@pruge I added react-dom and it works fine now.
I think it's an error that occurs cjs of recoil is included in the bundle.

@pruge
Copy link

pruge commented Jan 8, 2022

Hi @zenato thanks for replying.

I tried using @apollo/client@3.5.6 and the same error occurred.

@eeshankeni
Copy link

If anyones trying to do this on expo try.

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

module.exports = (async () => {
  const {
    resolver: {sourceExts},
  } = await getDefaultConfig(__dirname);;
  return {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
    },
    resolver: {
      sourceExts: [...sourceExts, 'cjs'],
    },
  };
})();

@ghost
Copy link

ghost commented Mar 28, 2022

Screen Shot 2022-03-28 at 21 07 30
Still get the same error, even after added the metro.config.js

@mydesweb
Copy link

mydesweb commented Apr 3, 2022

It is working just fine, this is my setup

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */
const {getDefaultConfig} = require('metro-config');
const {resolver: defaultResolver} = getDefaultConfig.getDefaultValues();

module.exports = {
  resolver: {
    ...defaultResolver,
    sourceExts: [...defaultResolver.sourceExts, 'cjs'],
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
};

then run npx react-native start --reset-cache

@oxbits
Copy link

oxbits commented Apr 30, 2022

These workarounds break my AWS Amplify Cognito Authentication in React Native.

Will there be a fix in the future that does not require the workaround?

@oxbits
Copy link

oxbits commented Apr 30, 2022

AWS closed the same issue on their end because they consider it a third party library issue. Seems like we are getting stuck in a catch 22 situation.

aws-amplify/amplify-js#9599

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants