`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
.jsextension with.mjsinbuild/esm.js, but obviously this will cause breaking changes for scenarios that already use ESM files. - We can also publish a new
node-esmdirectory for.mjsfiles for Node.js. Butnode-esmis not a good name for forward compatibility reasons. - Further, as described in #1314, continue to push the implementation of the
dayjs-esmpackage, just likelodash-es.- If we take this approach, we also no longer need to use the
.mjsextension. Just keep using.js.
- If we take this approach, we also no longer need to use the
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.
Any news here?
2.0 supports ESM
2.0 supports ESM
import dayjs from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.mjs'
console.log(dayjs()) // It works 👍🏼
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?
I'm also waiting for Node.js ESM support
Would be great to get ESM support!
Another vote here for ESM support.
Come on! Where is ESM support?
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.
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.
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
Another vote here for ESM support.
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.
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:
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).
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.