Apollo-client: [3.0.0-beta.24] Module not found: Can't resolve 'apollo-client' in 'node_modules/@apollo/react-hooks/lib'

Created on 18 Jan 2020  路  17Comments  路  Source: apollographql/apollo-client

Upgraded to 3.0.0-beta, everything worked until I did a hard restart of my create-react-app. Upon running my app, I receive the following error:

./node_modules/@apollo/react-hooks/lib/react-hooks.esm.js
Module not found: Can't resolve 'apollo-client' in '/Users/bstrom/dev/reviewtool/node_modules/@apollo/react-hooks/lib'

Versions
System:
OS: macOS Mojave 10.14.6
Binaries:
Node: 12.13.0 - ~/.nvm/versions/node/v12.13.0/bin/node
Yarn: 1.21.1 - /usr/local/bin/yarn
npm: 6.12.0 - ~/.nvm/versions/node/v12.13.0/bin/npm
Browsers:
Chrome: 79.0.3945.117
Firefox: 72.0.1
Safari: 13.0.4
npmPackages:
@apollo/client: ^3.0.0-beta.24 => 3.0.0-beta.24
@apollo/react-hooks: ^3.1.3 => 3.1.3
@apollo/react-testing: ^3.1.3 => 3.1.3
apollo-cache-inmemory: ^1.6.5 => 1.6.5
apollo-link: ^1.2.13 => 1.2.13
apollo-link-error: ^1.1.12 => 1.1.12
apollo-link-http: ^1.5.16 => 1.5.16
apollo-server: ^2.9.7 => 2.9.15
-->

馃摑 documentation

Most helpful comment

@bstro we haven't quite finished our migration guide yet (it should be done this upcoming week), but React Apollo's hooks are now included in @apollo/client. You can (and should) drop your @apollo/react-hooks dependency, and instead import directly from @apollo/client.

All 17 comments

@bstro we haven't quite finished our migration guide yet (it should be done this upcoming week), but React Apollo's hooks are now included in @apollo/client. You can (and should) drop your @apollo/react-hooks dependency, and instead import directly from @apollo/client.

Thanks @hwillson !

This solves my issue but I noticed you added this to your release milestone so I'll leave this open. Thank you for getting back to me so quickly.

Great - I'm planning on keeping this open until the migration guide is ready. Thanks!

@hwillson I'm getting this very same error on Windows 10 Pro, even though I've installed @apollo/client 3.0.0-beta.24 and dropped @apollo/react-hooks 3.0.0.

Is this a known issue on windows?

@TheoMer Are you using next-with-apollo? I was facing this issue today. You'll need to install the latest 5.0 version.
Because some of my code is still not refactored to hooks I also needed to use @apollo/react-components@beta.
Also if you want to use SSR you need to install @apollo/react-ssr@beta and then import getDataFromTree and pass it:

```
import withApollo from 'next-with-apollo';
import { getDataFromTree } from '@apollo/react-ssr';
import { notification } from 'antd';
import {
ApolloClient,
ApolloLink,
HttpLink,
InMemoryCache,
} from '@apollo/client';
import { onError } from '@apollo/link-error';

export default withApollo(
({ ctx, headers, initialState }) =>
new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors) {
// Do something with the errors
graphQLErrors.forEach(error => {
// console.log(error);
console.error(
[GraphQL error]: Message: ${error.message}, Location: ${error.locations}, Path: ${error.path}
);
notification.error({
message: 'Error',
description: error.message,
});
});
} else if (networkError) {
console.error([Network error]: ${networkError});
}
}),
new HttpLink({
uri: process.env.NODE_ENV === 'development' ? endpoint : backendUrl,
credentials: 'include',
headers: {
cookie: headers && headers.cookie,
},
}),
]),
cache: new InMemoryCache({}).restore(initialState || {}),
}),
{ getDataFromTree }
);

@chemicalkosek Yes, I was using [email protected] for withApollo and only by chance resolved an issue by importing it from [email protected] instead.

I am experiencing same issue:
./node_modules/@apollo/react-hooks/lib/react-hooks.esm.js Module not found: Can't resolve 'apollo-client' in 'node_modules\@apollo\react-hooks\lib'

@mshahbazm did you try removing react-hooks package ? see @hwillson message up top

My package.json does not have the react-hooks package, only the @apollo/client. For some reason, I'm getting the error. When I do a search all, react-hooks _is_ in my yarn.lock. And, furthermore, in my node_modules that the error references, there is react-hooks. Any ideas?

@hatchli @mshahbazm I solved this by making sure that my other packages are also on the latest version i.e react-components are on 4.0.0 etc. Check migration document and upgrade all other @apollo/* packages all of them do not work with each other

Thank you @mhuconcern ! Based on what you said, I looked into it further and realized there was a beta version of react-ssr ( "@apollo/react-ssr": "^4.0.0-beta.1"), and that fixed it upon updating. Seems like the old ssr had the hooks dependency, and that was my problem!

Thanks @mhuconcern , I could not solve it, I updated all packages. I am not sure how to get react-components v4. even with @latest flag I am getting its v3.1
So after spending a day I moved to stable version or apollo client :D

@mshahbazm because @latest is stable. you need to use @beta

npm install @apollo/react-components@beta

You can always check what versions are available on the Versions Tab on npm:
https://www.npmjs.com/package/@apollo/react-components?activeTab=versions

@chemicalkosek @mhuconcern
Finally I have also moved to v3 as well. Thanks to both of you.

The original issue here has been resolved, and the Migration Guide has been updated.

Finally problem solved!

Following is info from Apollo migration guide:

@apollo/react-hooks
All @apollo/react-hooks functionality is included in the @apollo/client package. For example:

import { ApolloProvider, useQuery, useApolloClient } from '@apollo/client'
As part of migrating, we recommend removing all @apollo/react-hooks dependencies.

Basically, your package.json should only have @apollo/client package (within apollo packages context). Most of the commonly used packages(like apollo-boost, react-apollo, @apollo/react-components, @apollo/react-hooks ) are either included in @apollo/client or retired. Please refer to the docs here

My project:

package.json:
```
"dependencies": {
"@apollo/client": "3.1.3",
"@fluentui/react-northstar": "^0.51.0",
"@reach/router": "^1.3.3",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"graphql": "^15.3.0",
"graphql-tag": "^2.10.1",
"lodash": "^4.17.20",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"react-table": "^7.5.1",
"typescript": "^3.8.3"
},

index.tsx

import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";

const cache = new InMemoryCache();

export const client: ApolloClient = new ApolloClient({
cache,
uri: "http://localhost:8881/graphql",
typeDefs: clientSideSchema as any,
});
......
.........
ReactDOM.render(

,
document.getElementById("root")
);

ExampleComponent.tsx --> get hold of apollo client object using useApolloClient hook:

import { useApolloClient } from "@apollo/client";

const UsersQueryContainer = (props: IUserQueryProps) => {
const client = useApolloClient();
const {
data: users,
loading: usersLoading,
error: usersError,
} = useUsersQuery({
variables: {
usersType: "members",
},
});

return ;
};
```

Was this page helpful?
0 / 5 - 0 ratings