iamkun / dayjs

ā° Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.71k stars 2.28k forks source link

`dayjs` does not actually support Node.js ESM #1765

Open CyanSalt opened 2 years ago

CyanSalt commented 2 years ago

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:

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.

katerlouis commented 2 years ago

Any news here?

benmccann commented 2 years ago

2.0 supports ESM

steffanhalv commented 2 years ago

2.0 supports ESM

import dayjs from 'https://cdn.jsdelivr.net/npm/dayjs@2.0.0-alpha.2/dist/index.mjs'
console.log(dayjs()) // It works šŸ‘šŸ¼
michaelhays commented 1 year ago

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?

neilsoult commented 1 year ago

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

MonsterDeveloper commented 1 year ago

Any ETA on ESM arrival on NPM?

DaniilIsupov commented 1 year ago

I'm also waiting for Node.js ESM support

Lauro235 commented 11 months ago

Would be great to get ESM support!

andyg3 commented 11 months ago

Another vote here for ESM support.

n3ih7 commented 11 months ago

Come on! Where is ESM support?

0x80 commented 7 months ago

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 dayjs@1.11.10/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 commented 7 months ago

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.

blackfalcon commented 7 months ago

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

alex-arriaga commented 7 months ago

Another vote here for ESM support.

michaelhays commented 6 months ago

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:

blackfalcon commented 6 months ago

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.

tangxiangmin commented 4 months ago

looks bad but work fine šŸ™…šŸ»ā€ā™€ļø

image

Has there been any progress on this issue?

manchicken commented 3 months ago

@codyfrisch: With all of the folks forcing us to ESM, regardless of technical benefit, this seems like it's going to continue to be a problem.

manchicken commented 3 months ago

From @codyfrisch: You mean developers choosing to use an official ECMAScript standard that is intended to be universal between backend and frontend, supports async module loading, and tree-shaking is "forcing" you to not live in the past? Got it.

You're coming in hot, friend. I'm talking about the trend of module maintainers changing away from supporting commonjs to ESM-only, and suggesting that it's "where the ecosystem is going" while only considering the preferences TypeScript enthusiasts and React developers.

There are a number of folks using Node for automation and systems-level scripts, and it seems all of the front-end web enthusiasm is the only thing anybody really cares to consider when making these decisions.

manchicken commented 3 months ago

From @codyfrisch: Okay boomer. J/K. I've never advocated removing CJS entirely. But refusing to support ESM properly is a great way to see your library die. Only reason I keep dayjs around is because it adheres to the non-standard formatting strings moment used. (there is a unicode standard that date-fns follows, and I expect temporal will follow.)

And reality check 97% of my projects are node for automations and system level scripting. All has been ESM for well over a year. I've worked around DayJS by implementing a helper that just replaces Y with y and D with d in formatting strings and use date-fns. (Format input is user supplied and I need to support their legacy expectation.)

So maybe it's a hot take, but nothing is holding you back except yourself.

I'm sorry friend, but you being fine with something is not an adequate substitute for the broader community being considered in decision-making.

Folks making decisions to break away from one approach or another do have an impact on a great number of people, and as a maintainer of several projects myself I'm always careful to be cognizant of that fact. I am genuinely glad to hear that this time it wasn't you. As you continue your own work, I sincerely hope that you continue your streak of good luck.

How we treat each other does matter.

manchicken commented 3 months ago

looks bad but work fine šŸ™…šŸ»ā€ā™€ļø

image

Has there been any progress on this issue?

I confirm that this works.