I'm running into an error after installing Apollo when trying to run my React Native Expo app. I've tried deleting node-modules and re-installing, resetting cache, restarting computer, and still no luck.
Android Bundling failed 456ms While trying to resolve module
@apollo/client
from file >/mnt/c/Users/14044/Desktop/Coding/divii/client/App.tsx
, the package >/mnt/c/Users/14044/Desktop/Coding/divii/client/node_modules/@apollo/client/package.json
>was successfully found. However, this package itself specifies amain
module field that >could not be resolved >(/mnt/c/Users/14044/Desktop/Coding/divii/client/node_modules/@apollo/client/main.cjs
. Indeed, none of these files exist:
- /mnt/c/Users/14044/Desktop/Coding/divii/client/node_modules/@apollo/client/main.cjs(.native|.android.jsx|.native.jsx|.jsx|.android.js|.native.js|.js|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.json|.native.json|.json)
- /mnt/c/Users/14044/Desktop/Coding/divii/client/node_modules/@apollo/client/main.cjs/index(.native|.android.jsx|.native.jsx|.jsx|.android.js|.native.js|.js|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.json|.native.json|.json)
To use Apollo in a vanilla JS project, you instead need to use the entry point @apollo/client/core : import { ApolloClient, gql, InMemoryCache } from "@apollo/client/core"; This is documented, but was a little hard to find within the Apollo 3.0 migration guide.
Executing a query To run a query within a React component, call useQuery and pass it a GraphQL query string. When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use to render your UI.
At its core, Apollo Client is a state management library that happens to use GraphQL to interact with a remote server. Naturally, some application state doesn't require a remote server because it's entirely local.
This is a conflict between @apollo/client v3.5.4 and RN metro bundler.
As a workaround until this issue is resolved, you can configure Metro by creating a metro.config.js file in the root of your React Native project with following content:
const { getDefaultConfig } = require("metro-config");
const { resolver: defaultResolver } = getDefaultConfig.getDefaultValues();
exports.resolver = {
...defaultResolver,
sourceExts: [
...defaultResolver.sourceExts,
"cjs",
],
};
This workaround was posted on Apollo Github Releases page here.
Try downgrading to @apollo/client
3.4.16
. I just did a round of package updates and the 3.5.4
broke my build as well. I'm using the downgraded package with a downgraded version of graphql
lib as well -- 15.7.2
.
Those are the last versions that worked for me with the current version of Expo / RN.
Hope that helps you out!
This is an issue with the latest version of Apollo Client
(3.5.0 and up) and the way Metro bundler
works. You need to configure Metro to understand the .cjs
files used in @apollo/client
by creating a metro.config.js
in the root folder of your project.
Here is the link to a solution on the Apollo
releases page.
I tried the solution, it didn't work, the error was solved, but the build broke for other packages in the project, so I tried a similar but different approach
Here is my metro.config.js
const {getDefaultConfig} = require('metro-config');
module.exports = (async () => {
const {
resolver: {sourceExts},
} = await getDefaultConfig();
return {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
resolver: {
sourceExts: [...sourceExts, 'cjs'],
},
};
})();
For anyone using Expo and the new Apollo Client, you should update your metro.config.js
to look like this:
const { getDefaultConfig } = require('@expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolver.sourceExts.push('cjs');
module.exports = defaultConfig;
It's important to make sure you use the expo metro config defaults otherwise eas will complain at build time.
Had the same issue in a Vanilla/Bare React Native Project with Apollo Client 3.5.8
The project already had the default metro.config.js
I just modified it to the following code :
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'],
},
};
With this change the issue was resolved
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With