[GH-ISSUE #3260] ERR_REQUIRE_ESM when importing better-auth@1.2.11+ due to jose being ESM-only #9544

Closed
opened 2026-04-13 05:02:39 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @Duart3x on GitHub (Jul 4, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3260

Originally assigned to: @Kinfe123 on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a standard NestJS project using CommonJS (TypeScript, Node 20.x)
  2. Install better-auth version 1.2.11 or later
  3. Run the project and import/use better-auth
  4. Observe the runtime error thrown due to jose being ESM-only

Current vs. Expected behavior

Expected:
better-auth should work without runtime errors in CommonJS projects, as previous versions (≤ 1.2.10) did.

Actual:
Using better-auth@1.2.11+ in a CommonJS-based project throws the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module .../jose/dist/webapi/index.js from .../better-auth/... not supported.
Instead change the require of index.js in ...better-auth... to a dynamic import() which is available in all CommonJS modules.

This is caused by better-auth trying to require('jose'), which is an ESM-only package and cannot be used that way in CJS.

What version of Better Auth are you using?

1.2.11+

Provide environment information

- OS: Windows 11
- Node.js: v20.x
- Package manager: pnpm
- Project type: NestJS (CommonJS)

Which area(s) are affected? (Select all that apply)

Package

Additional context

📌 Temporary Workaround
We're currently pinned to better-auth@1.2.10, which does not have this issue.

Originally created by @Duart3x on GitHub (Jul 4, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3260 Originally assigned to: @Kinfe123 on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a standard NestJS project using CommonJS (TypeScript, Node 20.x) 2. Install better-auth version 1.2.11 or later 3. Run the project and import/use better-auth 4. Observe the runtime error thrown due to jose being ESM-only ### Current vs. Expected behavior **Expected:** `better-auth` should work without runtime errors in CommonJS projects, as previous versions (≤ 1.2.10) did. **Actual:** Using better-auth@1.2.11+ in a CommonJS-based project throws the following error: ``` Error [ERR_REQUIRE_ESM]: require() of ES Module .../jose/dist/webapi/index.js from .../better-auth/... not supported. Instead change the require of index.js in ...better-auth... to a dynamic import() which is available in all CommonJS modules. ``` This is caused by better-auth trying to `require('jose')`, which is an ESM-only package and cannot be used that way in CJS. ### What version of Better Auth are you using? 1.2.11+ ### Provide environment information ```bash - OS: Windows 11 - Node.js: v20.x - Package manager: pnpm - Project type: NestJS (CommonJS) ``` ### Which area(s) are affected? (Select all that apply) Package ### Additional context 📌 Temporary Workaround We're currently pinned to better-auth@1.2.10, which does not have this issue.
GiteaMirror added the locked label 2026-04-13 05:02:39 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 4, 2025):

Hi @Duart3x! I'm Dosu and I’m helping the better-auth team.

This is a known technical limitation: better-auth@1.2.11+ imports the jose library using ESM-only syntax throughout its codebase, and jose itself is ESM-only. Even though better-auth’s package.json tries to support both CommonJS and ESM entry points, all usages of jose are static ESM imports, so require('better-auth') in a CommonJS project will trigger ERR_REQUIRE_ESM at runtime. There’s no compatibility layer or dynamic import workaround implemented in the codebase, and no official guidance or roadmap for CJS support or migration has been published. Pinning to 1.2.10 is currently the only viable workaround for CommonJS projects. If you need to use newer versions, you’d have to migrate your project to ESM or explore custom dynamic import hacks, but these aren’t officially supported or documented by better-auth.
Sources:
jwt.ts import
plugins/jwt/index.ts import
package.json config

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

<!-- gh-comment-id:3036448377 --> @dosubot[bot] commented on GitHub (Jul 4, 2025): <!-- Greeting --> Hi @Duart3x! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known technical limitation: better-auth@1.2.11+ imports the jose library using ESM-only syntax throughout its codebase, and jose itself is ESM-only. Even though better-auth’s package.json tries to support both CommonJS and ESM entry points, all usages of jose are static ESM imports, so require('better-auth') in a CommonJS project will trigger ERR_REQUIRE_ESM at runtime. There’s no compatibility layer or dynamic import workaround implemented in the codebase, and no official guidance or roadmap for CJS support or migration has been published. Pinning to 1.2.10 is currently the only viable workaround for CommonJS projects. If you need to use newer versions, you’d have to migrate your project to ESM or explore custom dynamic import hacks, but these aren’t officially supported or documented by better-auth. Sources: [jwt.ts import](https://github.com/better-auth/better-auth/blob/93a5a84851c338c2bd238b515e965aae0df18f88/packages/better-auth/src/crypto/jwt.ts) [plugins/jwt/index.ts import](https://github.com/better-auth/better-auth/blob/93a5a84851c338c2bd238b515e965aae0df18f88/packages/better-auth/src/plugins/jwt/index.ts) [package.json config](https://github.com/better-auth/better-auth/blob/93a5a84851c338c2bd238b515e965aae0df18f88/packages/better-auth/package.json) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/73421151-fb3f-445a-a7cd-95621cb56f1a?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3260)
Author
Owner

@cjroebuck commented on GitHub (Jul 11, 2025):

I'm also seeing this when running vitest in an ESM project

<!-- gh-comment-id:3061780664 --> @cjroebuck commented on GitHub (Jul 11, 2025): I'm also seeing this when running vitest in an ESM project
Author
Owner

@cjroebuck commented on GitHub (Jul 11, 2025):

This can be fixed by updating node.js version:

CJS style let jose = require('jose') is possible in Node.js versions where the require(esm) feature is enabled by default (^20.19.0 || ^22.12.0 || >= 23.0.0). ↩2 ↩3

<!-- gh-comment-id:3061813620 --> @cjroebuck commented on GitHub (Jul 11, 2025): This can be fixed by updating node.js version: > CJS style let jose = require('jose') is possible in Node.js versions where the require(esm) feature is enabled by default (^20.19.0 || ^22.12.0 || >= 23.0.0). [↩](https://github.com/panva/jose#user-content-fnref-cjs-6eb11031676257148789c21b69255f97) [↩2](https://github.com/panva/jose#user-content-fnref-cjs-2-6eb11031676257148789c21b69255f97) [↩3](https://github.com/panva/jose#user-content-fnref-cjs-3-6eb11031676257148789c21b69255f97)
Author
Owner

@Duart3x commented on GitHub (Jul 11, 2025):

@cjroebuck I was using the version 20.11.0, updating the node-js version solved my problem. Thank you

This can be fixed by updating node.js version:

CJS style let jose = require('jose') is possible in Node.js versions where the require(esm) feature is enabled by default (^20.19.0 || ^22.12.0 || >= 23.0.0). ↩2 ↩3

<!-- gh-comment-id:3062588629 --> @Duart3x commented on GitHub (Jul 11, 2025): @cjroebuck I was using the version 20.11.0, updating the node-js version solved my problem. Thank you > This can be fixed by updating node.js version: > > > CJS style let jose = require('jose') is possible in Node.js versions where the require(esm) feature is enabled by default (^20.19.0 || ^22.12.0 || >= 23.0.0). [↩](https://github.com/panva/jose#user-content-fnref-cjs-6eb11031676257148789c21b69255f97) [↩2](https://github.com/panva/jose#user-content-fnref-cjs-2-6eb11031676257148789c21b69255f97) [↩3](https://github.com/panva/jose#user-content-fnref-cjs-3-6eb11031676257148789c21b69255f97)
Author
Owner

@shahneil commented on GitHub (Jul 14, 2025):

I'm on Node 24.3.0 but still having this issue

<!-- gh-comment-id:3071347965 --> @shahneil commented on GitHub (Jul 14, 2025): I'm on Node 24.3.0 but still having this issue
Author
Owner

@imRahul05 commented on GitHub (Oct 26, 2025):

yes using node 22.12.0 solved my issue , thanks...

<!-- gh-comment-id:3448554906 --> @imRahul05 commented on GitHub (Oct 26, 2025): yes using node 22.12.0 solved my issue , thanks...
Author
Owner

@Yab112 commented on GitHub (Nov 23, 2025):

Just update the version and it will work.

<!-- gh-comment-id:3568042187 --> @Yab112 commented on GitHub (Nov 23, 2025): Just update the version and it will work.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9544