Skip to content

Switch to ESM in (applicable) TypeScript packages #10972

@SuperchupuDev

Description

@SuperchupuDev

Which application or package is this feature request for?

brokers, builders, collection, core, formatters, next, proxy, rest, structures, util, voice, ws

Feature

Since all packages seem to now require node >=22.12.0, I'm proposing dropping the CJS builds for at least the TypeScript packages (not proposing it for discord.js initally since it doesn't use a bundler and would be more work, likely affecting every file). Node 22.12.0 unflagged support for requireing ESM, which is currently in a release candidate status with plans to make it stable quite soon. Currently all relevant TypeScript packages ship two versions of the same library, one in CJS and the other in ESM. This isn't ideal not just because it makes installations 2x larger, it also makes those packages vulnerable to the dual package hazard. I see zero drawbacks to dropping CJS builds considering nothing would change for users and they would still be able to use the library the same way as always, even if they use CJS.

Ideal solution or implementation

Considering tsup is used, this should be as easy as dropping 'cjs' from the format array in the config file, and changing every applicable package.json.

Alternative solutions or implementations

If for some strange reason it is decided to keep CJS builds despite the fact that users using the minimum required node version can require(esm) with absolutely no issues, at the very least the module-sync export condition could be used to avoid the dual package hazard.

"module-sync" - matches no matter the package is loaded via import, import() or require(). The format is expected to be ES modules that does not contain top-level await in its module graph - if it does, ERR_REQUIRE_ASYNC_MODULE will be thrown when the module is require()-ed.

Other context

I have a PR basically ready but I would like to first ask for clarification of which packages should be moved to ESM before submitting, especially around internal packages.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions