-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Feature Request
Is your feature request related to a problem? Please describe.
I intend to move most of my 1K+ packages to pure ESM within 2021. I’m hoping other package maintainers will follow.
Node.js 10 reached EOL in the end of April, and this month many packages have since been converted to ESM only. Although NestJS uses ESM-style imports in TypeScript, the TypeScript compiler still emits CommonJS modules. Attempting to require such packages will produce a confusing error:
Uncaught:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: node_modules/quick-lru/index.js
This is confusing since I’m already using the import statement to import my modules: import QuickLRU from 'quick-lru'. It is not until I look at the compiler’s output to realize that TypeScript converted into require statements.
Describe the solution you'd like
Nest.js should be able to import pure-ESM packages.
There are many ways to solve this problem:
-
Make TypeScript emit ESM code instead of CommonJS: This will challenging for a while because it is still a hassle to fully embrace ESM, especially considering that file extensions are now required and we must use
.jseven though we import.tsfiles. -
Use a bundler to import ESM modules. Unfortunately this adds a bundling step.
-
Monkey-patch the runtime to allow seamless importing of ESM, e.g. by using
esmpackage?
Teachability, Documentation, Adoption, Migration Strategy
This would make developers less confused when importing pure ESM packages from Nest.js projects.
What is the motivation / use case for changing the behavior?
I tried to require quick-lru module which is now ESM only.