dayjs: `dayjs` does not actually support Node.js ESM

Node.js has implemented native support for ESM in the real world, and the community is increasingly recommending the use of ESM instead of CommonJS (e.g: https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77).

I noticed that dayjs actually publishes the esm directory, which contains the compiled product of the ESM syntax, see #1298. Although the "module" field of package.json was removed in #1314, the contents of esm are actually still available in the <script type="module"> in browsers or in bundling tools such as webpack.

Unfortunately, Node.js requires more than that for ESM. Since this is a CommonJS package, the ESM files it provides must use the .mjs extension to be properly parsed by Node.js.

I can get a few ways to solve this problem, but perhaps none of them are very good:

  • The easiest way to make it work is to replace the .js extension with .mjs in build/esm.js, but obviously this will cause breaking changes for scenarios that already use ESM files.
  • We can also publish a new node-esm directory for .mjs files for Node.js. But node-esm is not a good name for forward compatibility reasons.
  • Further, as described in #1314, continue to push the implementation of the dayjs-esm package, just like lodash-es.
    • If we take this approach, we also no longer need to use the .mjs extension. Just keep using .js.

I’m not sure if you have plans for this in terms of Node.js ESM, but I think that this is the way to go.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 55
  • Comments: 18

Commits related to this issue

Most upvoted comments

Come on! Where is ESM support?

Looks like the 2.0 alpha may be abandoned – no new commits or releases in 6 months.

Is there a plan to re-open work on 2.0, or to bring ESM support to 1.0?

Looks like the 2.0 alpha may be abandoned – no new commits or releases in 6 months.

Is there a plan to re-open work on 2.0, or to bring ESM support to 1.0?

BUMP

Any ETA on ESM arrival on NPM?

Two years on and nothing on proper ESM support tells me this is a dead end project and I need to migrate my code to another library. ESM is the only way forward, that is written on the wall.

2.0 supports ESM

Two years on and nothing on proper ESM support tells me this is a dead end project and I need to migrate my code to another library. ESM is the only way forward, that is written on the wall.

Pretty much, I’m just waiting patiently for https://github.com/tc39/proposal-temporal to hit Stage 4 so I can migrate my Day.js code to that šŸ™