Skip to content

fix: add CommonJS support via "require" export in package.json#139

Merged
techniq merged 4 commits intotechniq:mainfrom
abcdannyyoo:fix/cjs-support
Aug 7, 2025
Merged

fix: add CommonJS support via "require" export in package.json#139
techniq merged 4 commits intotechniq:mainfrom
abcdannyyoo:fix/cjs-support

Conversation

@abcdannyyoo
Copy link

Hey team,

I came across an issue when trying to use buildQuery from "odata-query" in a CommonJS project, importing it like this:
import buildQuery from "odata-query";

which throws an error:
Error: No "exports" main defined in odata-query/package.json

This PR adds "require": "./dist/index.js.mjs" to package.json

I believe this change allows CommonJS compatibility while preserving existing ESM behavior.

@techniq
Copy link
Owner

techniq commented Aug 5, 2025

Hi @abcdannyyoo 👋

We dropped CJS support in 8.x after switching from browserify/jest to vite/vitest (and later dropping the tsconfig for cjs builds).

Adding it back will require a separate build step to produce a .cjs file, along with the exports update similar to your PR. While I would like to drop CJS support and not look back, I would consider re-adding if a PR is submitted and requires minimal maintenance.

@abcdannyyoo
Copy link
Author

Hi @techniq,

Thanks for the context! I've updated the PR with a Vite config that now produces both ESM and CJS outputs.

It might sound a bit silly, but I noticed there were no actual .mjs files being generated under the dist folder, so I've updated package.json to reference the correct filenames (.cjs.js and .es.js).

@techniq
Copy link
Owner

techniq commented Aug 5, 2025

Hi @abcdannyyoo. There is a .mjs file produced with the current setup...

image

...and is the recommend extension to help Node.js distinguish between CommonJS (.cjs) and ESM (.mjs). I would like to keep this convention.

@enure performed the previous Vite migration and tsconfig cleanup and might have some suggestions here as well. I also don't want to break any current ESM use cases at the expense of re-supporting CJS.

@abcdannyyoo
Copy link
Author

abcdannyyoo commented Aug 5, 2025

@techniq Ah, I wasn't entirely sure how index.js.mjs ended up being generated, especially since the Vite config explicitly set fileName to index.js. I'm guessing this is due to how Vite interprets the combination of:

formats: ['es']
fileName: 'index.js'

I'll give it another go and make some changes sometime today.

@abcdannyyoo
Copy link
Author

@techniq Just pushed an update reverting the ESM output back to .mjs. This keeps things compatible for existing ESM consumers while adding support for CommonJS. Would be great to get a sanity check from any Vite experts to ensure this setup is solid.

@techniq
Copy link
Owner

techniq commented Aug 5, 2025

Thanks @abcdannyyoo, I appreciate the iterations. I took a quick look and everything looks in order. I noticed 2 warnings in the console.

image

Looks like we can remove build.lib.formats since we have build.rollupOptions.output defined with explicit filenames.

Also, the warning about named and default exports is rather interesting. Been a lot of discussion on this in the past but never knew it was "intentional".

I'm going to see if others have any additional improvements, but looks good to me from a quick local check.

CommonJS ESM
image image

@techniq
Copy link
Owner

techniq commented Aug 5, 2025

@abcdannyyoo actually, can you rename index.js.mjs to just index.mjs and index.cjs.js to index.cjs (and update the package.json exports accordingly). Thanks!

@abcdannyyoo
Copy link
Author

@techniq Done ✌️

@techniq
Copy link
Owner

techniq commented Aug 6, 2025

Thanks again @abcdannyyoo. One last request and should be it. Can you add a changeset (pnpm changeset) which will update the CHANGELOG and bump the version (which is used by CI for releases). Something like: fix: Restore CommonJS support.

@abcdannyyoo
Copy link
Author

@techniq All good. Just added the changeset file. Let me know if there's anything else.

Copy link
Owner

@techniq techniq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again @abcdannyyoo

@techniq techniq merged commit d832186 into techniq:main Aug 7, 2025
1 check passed
@github-actions github-actions bot mentioned this pull request Aug 7, 2025
@techniq
Copy link
Owner

techniq commented Aug 7, 2025

Released as 8.0.4

@abcdannyyoo
Copy link
Author

@techniq Appreciate the merge and thanks for being open to reintroduce cjs support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants