Error: While trying to resolve module @apollo/client React Native
Asked Answered
C

4

9

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:

Dependencies

"@apollo/client": "^3.3.2",
"graphql": "^15.4.0",

Anyone can help me please? Will be very thankful!

Centenary answered 13/12, 2021 at 16:57 Comment(0)
P
14

As documented at https://github.com/apollographql/apollo-client/blob/main/CHANGELOG.md#apollo-client-354-2021-11-19, the solution should be to add

const { getDefaultConfig } = require("metro-config");
const { resolver: defaultResolver } = getDefaultConfig.getDefaultValues();
exports.resolver = {
  ...defaultResolver,
  sourceExts: [
    ...defaultResolver.sourceExts,
    "cjs",
  ],
};

in your metro.config.js.

In my case, I already have a module.exports generated by default, so I just had to make the file so:

const {getDefaultConfig} = require('metro-config');
const {resolver: defaultResolver} = getDefaultConfig.getDefaultValues();

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

Pevzner answered 24/12, 2021 at 12:37 Comment(1)
Great! This worked for me.Rumery
I
3

Simply adding cjs file extension to metro.config.js works for me.

According to expo's Official Adding more file extensions to assetExts documentation...

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

const defaultConfig = getDefaultConfig(__dirname);

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

module.exports = defaultConfig;
Inhibition answered 14/12, 2021 at 16:14 Comment(1)
insteadof assetExts, sourceExts worked for meMccurdy
G
3

I have exactly the same problem in react-native. From the documentation, it follows that you need to add the ability to handle "cjs" files.

https://github.com/apollographql/apollo-client/blob/main/CHANGELOG.md#apollo-client-354-2021-11-19

Solved the problem today by adding to node_modules/metro-config/src/defaults/defaults.js

export.sourceExts = ["js", "json", "ts", "tsx", "cjs"];

and from the project folder for android:

cd android && ./gradlew clean

for ios in xcode :

clean -> run

Gatling answered 14/12, 2021 at 16:25 Comment(0)
B
2

For Expo Projects, We need to add cjs to sourceExts.

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

defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;

Apollo Docs for source extension https://github.com/apollographql/apollo-client/blob/main/CHANGELOG.md#apollo-client-354-2021-11-19

Expo syntax for metro config https://docs.expo.dev/guides/customizing-metro/

Bullwhip answered 1/3, 2022 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.