-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Importing @apollo/client
fails in Node
#9925
Comments
Try importing from |
Thanks! Importing
|
@justinfagnani instead of importing from an Apollo Client index module it's better to deep import the exact module you need, e.g: // Note that this example imports an ESM module.
import { ApolloLink } from "@apollo/client/link/core/ApolloLink.js"; For reasons why importing from an index module is an anti-pattern, see: https://jaydenseric.com/blog/optimal-javascript-module-design But, I'm also running into problems with broken types with TypeScript config |
Hi @justinfagnani 👋 Hopefully Jayden's fix unblocked you here—I'll go ahead and close this issue for now but feel free to re-open it if needed. Thanks! |
Please re-open, this is not working on the latest version. There is still an dependency on react. import { ApolloClient } from "@apollo/client/core/ApolloClient.js";
import pkg from '@apollo/client';
const { gql } = pkg;
const apollo = new ApolloClient({
link: new HttpLink({ uri: 'https://gql.metaspan.io/graphql', fetch }),
cache: new InMemoryCache(),
}); % node test.js
node:internal/modules/cjs/loader:956
const err = new Error(message);
^
Error: Cannot find module 'react'
Require stack:
- /.../proto/node_modules/@apollo/client/react/context/context.cjs
- /.../proto/node_modules/@apollo/client/react/react.cjs
- /.../proto/node_modules/@apollo/client/main.cjs
at Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
at Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/.../proto/node_modules/@apollo/client/react/context/context.cjs:6:13)
at Module._compile (node:internal/modules/cjs/loader:1120:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
at Module.load (node:internal/modules/cjs/loader:998:32)
at Module._load (node:internal/modules/cjs/loader:839:12)
at Module.require (node:internal/modules/cjs/loader:1022:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/.../proto/node_modules/@apollo/client/react/context/context.cjs',
'/.../proto/node_modules/@apollo/client/react/react.cjs',
'/.../proto/node_modules/@apollo/client/main.cjs'
]
}
Node.js v18.7.0
removing the "type": "module" from package.json and switching to require() seems to work
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Intended outcome:
Importing and using ApolloClient in Node works
Actual outcome:
An error:
Cannot find module 'react'
How to reproduce the issue:
This code seems like it should have a chance of working:
But it triggers the error:
Versions
System:
OS: macOS 12.4
Binaries:
Node: 18.6.0 - ~/.nvm/versions/node/v18.6.0/bin/node
npm: 8.14.0 - ~/.nvm/versions/node/v18.6.0/bin/npm
Browsers:
Chrome: 103.0.5060.134
Firefox: 102.0.1
Safari: 15.5
The text was updated successfully, but these errors were encountered: