Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 419
Dragos UniqQum Avatar asked May 10 '26 18:05

Dragos UniqQum


2 Answers

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'],
  },
};

like image 199
xji Avatar answered May 12 '26 11:05

xji


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;
like image 28
Ali Virk Avatar answered May 12 '26 09:05

Ali Virk