-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add Node/Browser ESM support to @apollo/client. #287
Comments
This could also be accomplished by switching all generated module files in the import { ApolloClient } from '@apollo/client/core/ApolloClient.js'; This could be fixed by keeping the .js files and duplicating them with the .mjs extension. I used this to get Apollo working in a esbuild project that only works with native ESM. |
Hi, this is problematic for us at Vulcan since we have moved our package library, that depends on @apollo/client, to ESM. This StackOverflow question sums it up: https://stackoverflow.com/questions/70615613/apollo-client-named-export-remove-not-found Next.js, which is based on Webpack 5, doesn't seem to support just having "exports": {
".": "./index.js",
"./link/error": "./link/error/index.js"
}, And also in ts-invariant package.json:
And finally to ts-invariant/process package.json:
|
Here is a gist: https://gist.github.com/eric-burel/543bdc809bcc62208deabdeb004db724 |
Any updates here? A year later it's still an issue when trying to use @apollo/client with something like Nuxt on Node 16
|
It seems that exports map is not an option for Apollo because it would break older applications, so the best hope seems to figure out why bundlers are not supporting this. Next has the same issue and rely on Webpack 5, I think that's also the case for Nuxt according to what I've read online, and maybe other frameworks |
@eric-burel Why not just convert the imports to the format shown in the error message? |
@benjamn can maybe have more info I am not sure it's that easy, when your package have a mix of ES modules that can in turn import modules that are either CJS or ESM, you are up to trouble. |
This breaks with Jest 28 as well, I had to reintroduce @apollo/client and ts-invariant in the build:
and also extend my patch to provide a full export map for all internal packages of @apollo/client. Finally, I needed to import "gql" from "graphql-tag" instead of @apollo/client. Demo: VulcanJS/vulcan-npm#115 |
I modified the package.json because I had to use esm only modules(ex: unified, remark ...)
a detour by applying it like this in the Jest test // <rootDir>/lib/test/apollo.cjs
const apollo = require('@apollo/client/main');
const gql = require('graphql-tag');
module.exports = { ...apollo, gql }; // jest.config.js
...
moduleNameMapper: {
'^@apollo/client$': '<rootDir>/lib/test/apollo',
}, with ts-jest // jest.config.js
...
preset: 'ts-jest/presets/js-with-ts-esm',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
}, // import { ApolloClient, InMemoryCache, NormalizedCacheObject, from, HttpLink } from '@apollo/client';
import { ApolloClient, InMemoryCache, NormalizedCacheObject } from '@apollo/client/core';
import { from } from '@apollo/client/link/core';
import { HttpLink } from '@apollo/client/link/http'; |
Would really appreciate an update on this. Currently using it with nuxt 3 |
Node 16 SSR ESM project: we point to the internal import { ApolloClient, from, gql } from '@apollo/client/core/index.js';
import { HttpLink } from '@apollo/client/link/http/index.js';
import { InMemoryCache } from '@apollo/client/cache/index.js';
import { createPersistedQueryLink } from '@apollo/client/link/persisted-queries/index.js'; |
@underblob Thank you, this seems to work. |
My patch doesn't work anymore with PNPM symlinking. I am able to provide open source repro, Apollo is really hard to use iin ESM packages... Edit: |
This works but indeed the sign that "@apollo/client/index.js" is not properly interpreted as an ES module in the first place. I have no idea how to force build system to take that into consideration. Maybe renaming to .mjs? Edit: doesn't work actually, it will still consider relative import as CJS (eg choke on "../versions.js"), I have a (big) repro here: VulcanJS/vulcan-npm#140 |
If using esbuild you can tell it to prefer ESM exports with the |
What about doing a major release version, which is how |
Wanted to note a similar but unique challenge here: I'm using import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
// ... codegen types and results When I try to use any of the exported values from the codegen file in an ESM Typescript project (otherwise working), I receive this error: import { gql } from '@apollo/client';
^^^
SyntaxError: Named export 'gql' not found. The requested module '@apollo/client' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from '@apollo/client';
const { gql } = pkg; Changing the imports as suggested makes it work, but I don't have control over the codegen plugin. |
Hi, I ran into this error as well. Will we get this fix soon? |
Isn't just the issue the fact that Typescript def looks like |
TypeScript 5 will magically discover the index file if moduleResolution is set to 'bundler' |
If someone sees this, this is what helped me resolve @apollo/client error in react-native. Thanks @revmischa
|
@Milutin-P worked like a charm! Thanks! |
This is a tracking issue for the work to add native ESM support for new Node versions and direct
<script type="module">
references for the@apollo/client
package.This would probably mean adding
"type": "module"
topackage.json
files and potentially exports fields. It may also include changes to the build system and package artifacts. It is potentially a breaking change for unwitting developers who have imported files which we never intended to export.Maybe this is a low priority, especially given most bundlers and front-end tools conflate and handle CommonJS and ESModules out of box, and I’m not sure what percentage of users import
apollo-client
from ESM-capable node versions. Maybe someone doing SSR?Also note that this is a risky change to the codebase which will cause outages if we don’t dot our t’s and cross our i’s. See https://javascript.plainenglish.io/is-promise-post-mortem-cab807f18dcc, for instance.
@benjamn @hwillson @jcreighton
Related issues:
#81 #83
The text was updated successfully, but these errors were encountered: