You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
I suppose that we'll eventually migrate towards ESM modules, but there's no plan to do this atm. For now, if you must use ESM modules, I'd suggest moving forward with a bundler.
Feature Request
Is your feature request related to a problem? Please describe.
A lot of npm packages are now being published as “ESM-only” (shipping only ES modules with no CommonJS version) which makes it no longer requirable from CommonJS code.
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:
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 intorequire
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
.js
even though we import.ts
files.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
esm
package?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.The text was updated successfully, but these errors were encountered: