question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:13
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
brianccommented, Jan 5, 2022

yea submit a pr I’d love to take a look at it

5reactions
LJNeoncommented, Sep 27, 2020

I would be happy to open a pull request but wish to confirm interest from the maintainers first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does it take to support Node.js ESM? – The Guild
To add support ESM for Node.js, you have two alternatives: build your library into ESM Compatible modules with the extension .mjs , and...
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem is shifting towards ESM. We examine what lies in store for application and library authors, learn some of challenges ...
Read more >
How to use ESM on the web and in Node.js - ByteofDev
The simplest way to use ESM on the web is by utilizing the native support for it. For about over 95% of users...
Read more >
Using ECMAScript modules (ESM) with Node.js
Learn about using ES modules in Node.js today and get a closer look at how you can migrate your codebase to make use...
Read more >
ECMAScript modules | Node.js v19.3.0 Documentation
Node.js fully supports ECMAScript modules as they are currently specified and ... The Import Assertions proposal adds an inline syntax for module import ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found