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:

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)

Most upvoted comments

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.

if @nestjs/apollo were shipping ESM/both, this issue wouldn’t manifest since we’d be loading ESM in both cases?

There would still be two (very unlikely) cases where both versions could be loaded:

  • Because dynamic import() is available to both CJS and ESM, if a user had a CJS codebase but used dynamic import() to load @nestjs/apollo.
  • On the flip side, if someone has an ESM codebase but loads @nestjs/apollo using createRequire

Somewhat related: another contributor recently showed me this to help us get our dual typings correct, and it looks like they aren’t quite right for the sequelize repo. Is that something you’re already aware of? Is it possible to simultaneously publish dual typings and take the wrapper approach?

Absolutely. 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.

From what I gathered in the sequelize monorepo, it looks like the build script is generating that es module wrapper via esbuild?

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’ll leave this open in case someone feels motivated to fix it though, and I’d be more than happy to review a PR.

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