Skip to content

Add ESM support #2353

@LJNeon

Description

@LJNeon

When importing a CommonJS library, it's flattened into a single object, and you're unable to take advantage of any ESM features. However if you add a few lines to your package.json and a esm.mjs wrapper file that simply re-exports the contents of index.js, then ESM users will be able to take full advantage of ESM features without any changes needed to your source code, and without affecting any CommonJS users. The package.json changes would be as simple as:

"main": "index.js",
"exports": {
  ".": [
    {"require": "index.js", "import": "esm.mjs"},
    "index.js"
  ],
  "./": "./"
}

The "index.js" fallback is only necessary to avoid package.json parsing errors in some older versions of Node, which would affect CommonJS users as well. If this library ever supports only Node 14+ in the future, the fallback could be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions