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.

Node.js ESM support

See original GitHub issue

Unfortunately, Node.js didn’t add support for "module" field that is supported by bundlers such as webpack, and introduced their own resolve logic.

To add Node.js ESM support instead of /esm submodule we need to add .mjs version next to CommonJS files. That would be a breaking change so we should introduce it in v3.

Thank you Node.js, very nice.

See the issue: https://github.com/date-fns/date-fns/issues/1779

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:16
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
karlhorkycommented, Aug 14, 2021

@kossnocorp maybe it would be an idea to move to pure ESM for v3, like Sindre Sorhus has been doing with his modules:

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

5reactions
marnuswcommented, Feb 24, 2022

@kossnocorp it is possible to support native Node.js ESM without breaking changes. I’ve just done so successfully in date-fns-tz. There are a few things to do:

  1. Change all imports to use the complete file names including the extension, e.g. ./format to ./format/index.js
  2. Add "type": "commonjs" to the main package.json
  3. Add a package.json inside the ./esm folder containing {"type": "module"}
  4. Change the build script so it adds "type": "module" to the package.json of each function in the esm folder
  5. Add the exports field to the main package.json similar to what I’ve done in date-fns-tz#package.json.

The last will be a proper mess because there are a gazillion files being exported from date-fns, but it should work…

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECMAScript modules | Node.js v19.3.0 Documentation
Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, ...
Read more >
What does it take to support Node.js ESM? – The Guild
ECMAScript modules, also known as ESM, is the official standard format to package JavaScript, and fortunately Node.js supports it .
Read more >
Getting Started with (and Surviving) Node.js ESM
Arguably the biggest feature and pain with Node.js' ESM support is not ESM itself, but rather the new exports field in package.json ,...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to...
Read more >
Using ECMAScript modules (ESM) with Node.js
Using official ES module support. Let's start with the first (and official) way provided by Node.js io use ES modules in your Node...
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