dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

`dayjs` does not actually support Node.js ESM

Open CyanSalt opened this issue 3 years ago • 21 comments

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.

CyanSalt avatar Jan 13 '22 13:01 CyanSalt

Any news here?

katerlouis avatar Mar 16 '22 15:03 katerlouis

2.0 supports ESM

benmccann avatar May 04 '22 03:05 benmccann

2.0 supports ESM

import dayjs from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.mjs'
console.log(dayjs()) // It works 👍🏼

steffanhalv avatar Aug 05 '22 09:08 steffanhalv

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?

michaelhays avatar Mar 20 '23 18:03 michaelhays

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

neilsoult avatar May 24 '23 14:05 neilsoult

Any ETA on ESM arrival on NPM?

MonsterDeveloper avatar Aug 29 '23 16:08 MonsterDeveloper

I'm also waiting for Node.js ESM support

DaniilIsupov avatar Sep 13 '23 13:09 DaniilIsupov

Would be great to get ESM support!

Lauro235 avatar Sep 29 '23 13:09 Lauro235

Another vote here for ESM support.

andyg3 avatar Sep 29 '23 14:09 andyg3

Come on! Where is ESM support?

n3ih7 avatar Oct 05 '23 16:10 n3ih7

Damn I just wasted hours on this 🤦‍♂️ When converting a large codebase to a monorepo, I kept getting errors like this at runtime:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/me/myproject/ services/fns/node_modules/dayjs/plugin/advancedFormat' imported from /Users/me/myproject/services/fns/dist/index.js Did you mean to import [email protected]/node_modules/dayjs/plugin/advancedFormat.js?

I couldn't figure it out because my bundled shared packages output looked correct, but I assumed it must be a mistake in some configuration. I tried everything I could think of before landing here.

I did not imagine a popular library like this would not have support for ESM yet in 2024.

I'll give the alpha 2.0 a try. Also found this issue describing that it should work by adding the .js import. It is unexpected, because extension prefixes are only required on ESM imports from relative paths, and never from an external library, but that must be a side effect of the funky not-really-esm exports.

0x80 avatar Feb 10 '24 21:02 0x80

The types in 2.0 alpha didn't seem to work for me. For example, TS didn't recognize that .utc() was available after applying the plugin. I ended up sticking .js to the v1 imports and that works for now.

0x80 avatar Feb 10 '24 22:02 0x80

Running SSR build with Svelte/Vite

(node:29857) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/dalesande/src/personal/sveltekit-auro/node_modules/dayjs/esm/index.js:1
import * as C from './constant';

Not sure if this is the fix you want, but I simply updated all the JS references to be .mjs and that fixed it. I pushed up a branch of the changes. The tests don't work, but thought this would be easy to see the diff.

https://github.com/iamkun/dayjs/compare/dev...blackfalcon:dayjs:dev

blackfalcon avatar Feb 13 '24 00:02 blackfalcon

Another vote here for ESM support.

alex-arriaga avatar Feb 19 '24 22:02 alex-arriaga

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.

codyfrisch avatar Mar 05 '24 20:03 codyfrisch

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 :pray:

michaelhays avatar Mar 05 '24 20:03 michaelhays

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 🙏

That would be ideal, but I have a requirement for format strings, and unfortunately I'm dealing with interop where there is an expectation for the moment.js/day.js style formatting strings. I suppose I could write something that converts between them... and use date-fns (I am assuming temporal is going to use unicode tokens like date-fns since its a standard).

codyfrisch avatar Mar 05 '24 21:03 codyfrisch

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.

The current version covers my use case, time to fork.

blackfalcon avatar Mar 07 '24 03:03 blackfalcon