#No support for ESM

18 messages · Page 1 of 1 (latest)

mystic loom
#

Hello,
my project is now failing build, because Nestjs is still not supporting ESM: https://github.com/nestjs/nest/pull/8736
Why was this closed and limited?
I will have to migrate away, since I won't be able to keep up with API changes in my project. This issue should not be closed.

GitHub

PR Checklist
Please check if your PR fulfills the following requirements:

The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
Tests for the chan...

night light
#

You can still import ESM packages inside of CJS. You need to use the await import('package') format and set the module resolution in the tsconfig to node16 if I recall correctly

#

The PR is not closed either, only locked, mainly because people kept pinging it and generating noise which is distracting for us. We'll get ESM working when the dependencies of the framework work with ESM as well 🙂

mystic loom
#

I agree, but this syntax is cumbersome and often doesn't work reliably.
This was mentioned countless times in the thread, and I don't get why it was ignored.

mystic loom
night light
#

Most of the time when I saw people in the issue mentioning that the await import wasn't working it was because of an incorrect syntax of getting the value out (default vs named exports). I will admit though that TS support for await import is rather poor, but that's able to be mitigated with using injection and custom providers for the import and then just import the type in the use place.

mystic loom
#

Okay, that is true. I think the syntax is pretty confusing 😅

#

It's sad to see the thread closed, but I guess I'll have to migrate to async imports.

night light
#

The pings would be an indicator, if there wasn't also support for how the await import syntax works which already exists in many other places.

The thread still isn't closed. It's just locked. The PR is still open and under consideration.

As Kamil had mentioned before, we have to make sure all of the things we depend on work in ESM too. That means Express, Fastify, Socket.io, ws, all of the microservice transports, mocha and jest, apollo, mercurius, TypeORM, Sequelize, Mongoose, and probably a few others I'm forgetting. It's a long checklist to work through and it's not a super high priority as it's really not a blocker with the factt that there is a way to work around it

mystic loom
#

That's understandable. I personally just don't like locked posts, as they keep people from communicating.
Anyhow, thanks for explaining this in detail.

#

Thanks for your time 👍

wanton gale
#

NestJS does support esm tho...
You just need to make some adjustments

#

I'm currently writing with type: module and es6

mystic loom
wanton gale
#

Oh no, I meant with regular imports

#

It works perfectly for me

#

In my package.json I defined "type": "module" and added this configuration:

    "extensionsToTreatAsEsm": [
      ".ts"
    ],
    "moduleNameMapper": {
      "^(\\.{1,2}/.*)\\.js$": "$1"
    },
    "transform": {
      "^.+\\.(t|j)s$": [
        "ts-jest",
        {
          "useESM": true
        }
      ]
    },

In tsconfig.json I defined "module": "ES2022" and... that's about it.
Now I can use import { IsString} from 'class-validator' for instance and it works fine...

latent fable
#

i am using this for importing esm modules and this works completely fine. I am using this to import nanoid . No problems so far. As jmcdo said, the issues are mostly due to syntax not the actual import itself . export const dynamicImport = async (packageName: string) => new Function(`return import('${packageName}')`)();