Skip to content

Support importing from pure ESM packages #7021

@dtinth

Description

@dtinth

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.

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:

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.

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