Skip to content

Enable ESM interoperability by default. #15331

Open
@codiophile

Description

@codiophile

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I was trying to use Octokit in a NestJS project, but the import didn't work properly. Octokit ended up just being an alias for any, which made the linter really unhappy and the auto-completion non-existent. I raised an issue with Octokit, because I thought they should make sure it works with CommonJS and I got an incredibly helpful response: octokit/octokit.js#2865

Describe the solution you'd like

Sine TypeScript 5.8, there is full interoperability between CommonJS and ESM. All you have to do is configure it. I changed my tsconfig.json to the following:

{
  "compilerOptions": {
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "resolvePackageJsonExports": true,
    "esModuleInterop": true,
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2023",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "noFallthroughCasesInSwitch": false
  }
}

It just works, and I don't see why it shouldn't be configured by default. Importing ESM has been such a pain for such a long time and there's now a solution that makes it completely seamless. I don't see why we shouldn't adopt it in NestJS.

Teachability, documentation, adoption, migration strategy

Assuming that this works with every part of NestJS (I've only tested the subset of modules I use in my project), this should be completely transparent to the end user. All the code they have today should keep working and if they ever try to import an ES module, it will just work and they don't have to worry about it.

I think this is a quick win and the main effort is to test it, to make sure that it works with every part of NestJS. In theory, it should, but I haven't tried it and I'm not an expert, so it seems like a reasonable thing to verify.

What is the motivation / use case for changing the behavior?

There's no noticeable change in behaviour. The only noticeable difference is that you can import ES modules and they just work as expected. It's purely an improvement, as far as I'm aware. At least in my project, it seems to work perfectly.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions