-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node.js ESM support #1781
Comments
@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 |
@kossnocorp it is possible to support native Node.js ESM without breaking changes. I've just done so successfully in
The last will be a proper mess because there are a gazillion files being exported from |
This PR should solve the ESM import for |
@karlhorky, personally, I'm not too fond of this approach. The only way for us to move forward is to have a dual package. I hope @ocavue's PR will work and the very next version will have ESM support. |
Node supports |
Issue on Nitro side: unjs/nitro#1889 |
Node.js ESM support is coming in v3. |
Nice, judging by the fact this issue is now closed, I guess v3 will be released imminently (in the next days)? Looking forward to it 🙌 |
@karlhorky nice try guilt tripping 🙌 Follow official Twitter for updates https://twitter.com/date_fns |
Oh, didn't mean to guilt-trip. It's just pretty common across open source to leave issues open until they are completed - otherwise, users will have a harder time finding them, may think that v3 is already released and may even open new issues because of confusion. Anyway, thanks for your effort here! |
He said it is already in main, so thats why it is closed. Lets see when v3 gets released :) I didnt see any info on Twitter tho. |
@karlhorky you're welcome! @MickL, as you might've guessed, I can't promise anything as it's a lot of effort and moving parts. If you follow the Twitter account, you'll know first when I have v3 ready. |
Saw on npm that |
@kossnocorp Btw
|
@karlhorky thank you for the heads up, but I'm not sure what this all means 🫠 I tested it on a TypeScript project, plus I added smoke tests that builds fine: https://github.com/date-fns/date-fns/tree/main/examples/typescript But I'll investigate more. |
Since you're aiming to dual-publish ESM and CJS (which I've heard is challenging to get right when "rolling your own"), you may want to consider trying out Other recommendations include |
@karlhorky thank you for the suggestion, but the current setup works very well now. Please check it out and tell me if you encounter any problems. |
This is what https://arethetypeswrong.github.io/?p=date-fns%403.0.0-alpha.1 is doing.
The grid that @karlhorky showed represents a matrix of TypeScript projects under different but valid (and common) configurations trying to import things from date-fns. Your smoke tests only cover the top-left four cells, which are all problem-free. The rest of the import paths, under the rest of the |
@andrewbranch thank you, I appreciate the offer! It was a challenge indeed to get around all the quirks, given that I had no idea about them before, but now it's all green: Anyone who wants to try, I shipped it as date-fns@3.0.0-alpha.2 |
Thank you all for the input. So do you advice using v3.0.0.0-alpha.2 to avoid adding to all the imports /index.js ? And other issues (as subfolders that don't have index.js). I'm having an issue with date-fns/fp at the moment |
There's date-fns@3.0.0-rc.2, it most likely will be the final release. |
Final as in the last release this lib will ever have?! |
@nskazki yes |
Thank you for the reply. I’ve looked around for an announcement, but haven’t found any. Should we expect one? Is there a short version? |
@nskazki yes, I'm working on a blog post. There's a change log in the repo. You can follow me or the official Twitter account for updates. |
Try to import some FP-version functions from import { startOfMonth, formatWithOptions } from 'date-fns/fp' Compile error Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/vercel/path0/frontend/my-app/node_modules/date-fns/fp' is not supported resolving ES modules imported from /vercel/path0/frontend/my-app/.svelte-kit/output/server/entries/pages/sales-bonus/_page.server.ts.js
Did you mean to import date-fns@2.30.0/node_modules/date-fns/fp/index.js?
at new NodeError (node:internal/errors:405:5)
at finalizeResolution (node:internal/modules/esm/resolve:320:17)
at moduleResolve (node:internal/modules/esm/resolve:946:10)
at defaultResolve (node:internal/modules/esm/resolve:1132:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:300:10)
at [kOnMessage] (node:internal/worker:311:37)
at MessagePort.<anonymous> (node:internal/worker:212:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:757:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28) {
code: 'ERR_UNSUPPORTED_DIR_IMPORT',
url: 'file:///vercel/path0/frontend/my-app/node_modules/date-fns/fp'
}
Node.js v18.18.2
ELIFECYCLE Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1 |
@alexander-mart v3 supports Node.js ESM |
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: #1779
The text was updated successfully, but these errors were encountered: