maraisr / diary

📑 Zero-dependency, fast logging library for Node, Browser and Workers
MIT License
252 stars 7 forks source link

fix: provide distinct suffix declaration for common js modules #23

Closed maxrawlinger closed 1 year ago

maxrawlinger commented 1 year ago

Error: require() of ES Module

Using diary as an dependency within another module, this module raises an error within a node/server side env (e.g. Next.js, Nuxt.js) see picture.

Bildschirm­foto 2023-03-13 um 09 18 54

Alternatively, as the error message describes, the file suffix could be still .js but the type declaration of the module itself should be changed to not being an ESM module.

Reproduction example: https://codesandbox.io/p/sandbox/quirky-bassi-7940pf

maraisr commented 1 year ago

Hey 👋🏻 thank you so much for the PR, not 100% certain on all this commonjs, type:module shenanigans.

I ran your codesandbox (thank you so much for that), and did a pnpm patch where I removed the type: module — and that seems to work.

Just give me a moment to understand the difference between .cjs and type: module. As surely the bundler should read import from the exports map, than use that.

maxrawlinger commented 1 year ago

Hello 👋 Thank you for your timely response.

If it helps in any way, please do not hesitate to contact me regarding the CommonJS/ESM hell :)

maraisr commented 1 year ago

Hey mate, I have just shipped diary@0.4.4 which should solve your requirements. Think in general type: module seems to break too many things upstream.

Think i'll stick to shipping cjs firstclass, with the .mjs if the bundler knows what to do with it. Hopefully that works for you now 🚀

Let me know how it goes. And thank you so much for submitting the PR in the first place <3