apollo-server: Different dists for CJS and ESM are causing both to be used, leading to TypeScript problems
Issue Description
@apollo/server provides different builds for ESM and CJS. It is great to see more ESM support, but the current approach means it suffers from the Dual package hazard described in the node documentation.
To sum up the hazard: this approach makes it extremely likely that both the ESM and CJS versions will be loaded at the same time. Here is a simple example of how this can happen:
- Your project is written in ESM.
- Your project uses
@nestjs/apollo. nestjs is written in commonJS and imports@apollo/server - Your project includes a plugin for apollo, so you also import
@apollo/server - Your project will load the ESM version of apollo, and nest will load the CJS version. Meaning the plugin cannot be compatible with nest.
I strongly recommend doing everything you can to only ship a single build to node users by either:
- Dropping support for CJS altogether
- Or using the “ES module wrapper” approach. The first approach described here: https://nodejs.org/api/packages.html#dual-commonjses-module-packages. Approach 2 suggested by node does not work for TypeScript users. Here is a real world example and a test that makes sure the wrapper is kept in sync
I’ve provided a minimalist example of what sort of errors this causes below
Link to Reproduction
https://codesandbox.io/p/sandbox/laughing-sun-85cmy8?file=%2Fsrc%2Findex.ts%3A1%2C1
Reproduction Steps
See above
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 11
- Comments: 16 (7 by maintainers)
Any updates on this?
Hi All! Sorry to cross-post <sort-of> but as I reported in (https://github.com/apollo-server-integrations/apollo-server-integration-aws-lambda/pull/152#issuecomment-1989575238) this is totally blocking us from upgrading from v3. Will the v3 deprecation be extended if this isn’t resolved soon? Has progress been made on this elsewhere and I just missed a ticket? Thanks for the excellent work!
@d3c0d3dpt I’d be curious to know how tree-shakeable Apollo Server actually is in order to form an opinion. Would you be able to try bundling Apollo Server as ESM and CJS in its current form to see how much it currently benefits from that?
I know that serverless users generally prefer the smallest bundle possible due to cold start time, so I don’t want to be insensitive to that.
There would still be two (very unlikely) cases where both versions could be loaded:
import()is available to both CJS and ESM, if a user had a CJS codebase but used dynamicimport()to load@nestjs/apollo.@nestjs/apollousingcreateRequireAbsolutely. We actually received the same report recently (https://github.com/sequelize/sequelize/issues/16157). Thankfully the fix is simple, we made the mistake of using the same typing file for both CJS and ESM. They can have the same content but they must have different file extensions.
We are writing our codebase in ESM, but we use esbuild to transpile it to CJS, and we have a small ESM wrapper file (that is not transpiled) that defines the ESM named exports
I’d be happy to open a PR to fix this. I think I’ll fix https://github.com/sequelize/sequelize/issues/16157 first to make sure we have a viable solution, then bring a similar setup over