SyntaxError: 'super' keyword unexpected #1851

Closed
opened 2026-03-13 09:08:07 -05:00 by GiteaMirror · 17 comments
Owner

Originally created by @nebrekab on GitHub (Sep 5, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I am seeing an issue with better-auth and Astro, running on Vite 7 with React.

The error is:

Image

Specifically that SyntaxError: 'super' keyword unexpected here...

I have found this thread that highlights the same issue (this time with NextJS and Prisma): https://www.reddit.com/r/better_auth/comments/1muowba/error_using_betterauth_with_nextjs_prisma/

Before the update to better-auth 1.3.8 (from 1.2.8) and Vite 7.1.4 (from 6.3.5), there was no issue. (Astro version is 5.13.5.)

package.json

{
  "name": "my-project",
  "type": "module",
  "version": "0.1.0",
  "scripts": {
    "dev": "astro dev",
    "build": "astro build",
    "preview": "astro preview",
    "astro": "astro",
    "cert": "rm -rf .cert && mkdir -p .cert && mkcert -key-file ./.cert/key.pem -cert-file ./.cert/cert.pem 'localhost'"
  },
  "devDependencies": {
    "@astrojs/netlify": "^6.5.9",
    "@astrojs/react": "^4.3.0",
    "@react-email/components": "^0.5.1",
    "@storyblok/astro": "^7.2.5",
    "@tailwindcss/vite": "^4.1.13",
    "@types/node": "^24.3.1",
    "@types/react": "^19.1.12",
    "@types/react-dom": "^19.1.9",
    "astro": "^5.13.5",
    "better-auth": "^1.3.8",
    "mysql2": "^3.14.4",
    "resend": "^6.0.2",
    "tailwindcss": "^4.0.7",
    "vite": "^7.1.4"
  },
  "dependencies": {
    "react": "^19.1.1",
    "react-dom": "^19.1.1"
  }
}

Current vs. Expected behavior

Currently React breaks immediately on page load. So the page stops working. I believe this is due to invalid syntax in the bundled markup. As mentioned in Additional content, bypassing Vites' dependency optimization gets around the error, but I think this is not the ideal/long term solution.

What version of Better Auth are you using?

1.3.8

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6020",
    "release": "24.6.0",
    "cpuCount": 12,
    "cpuModel": "Apple M2 Pro",
    "totalMemory": "32.00 GB",
    "freeMemory": "0.38 GB"
  },
  "node": {
    "version": "v22.17.1",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.5.2"
  },
  "frameworks": null,
  "databases": null,
  "betterAuth": {
    "version": "Unknown",
    "config": null,
    "error": "ENOENT: no such file or directory, open 'package.json'"
  }
}

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

Client

Auth config (if applicable)

not included as I believe this is a vite compiling issue

Additional context

I have managed to bypass the issue, by adding to exclude optimizing better-auth to my Vite (Astro, in this case) config:

vite: {
    server: {
      https: true
    },
    optimizeDeps: {
      exclude: ["better-auth"],
    },
  },
Originally created by @nebrekab on GitHub (Sep 5, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I am seeing an issue with better-auth and Astro, running on Vite 7 with React. The error is: <img width="669" height="210" alt="Image" src="https://github.com/user-attachments/assets/b3508eec-d91b-4028-ba8c-c70a7cc0d4c1" /> Specifically that SyntaxError: 'super' keyword unexpected here... I have found this thread that highlights the same issue (this time with NextJS and Prisma): https://www.reddit.com/r/better_auth/comments/1muowba/error_using_betterauth_with_nextjs_prisma/ Before the update to better-auth 1.3.8 (from 1.2.8) and Vite 7.1.4 (from 6.3.5), there was no issue. (Astro version is 5.13.5.) #### package.json ``` { "name": "my-project", "type": "module", "version": "0.1.0", "scripts": { "dev": "astro dev", "build": "astro build", "preview": "astro preview", "astro": "astro", "cert": "rm -rf .cert && mkdir -p .cert && mkcert -key-file ./.cert/key.pem -cert-file ./.cert/cert.pem 'localhost'" }, "devDependencies": { "@astrojs/netlify": "^6.5.9", "@astrojs/react": "^4.3.0", "@react-email/components": "^0.5.1", "@storyblok/astro": "^7.2.5", "@tailwindcss/vite": "^4.1.13", "@types/node": "^24.3.1", "@types/react": "^19.1.12", "@types/react-dom": "^19.1.9", "astro": "^5.13.5", "better-auth": "^1.3.8", "mysql2": "^3.14.4", "resend": "^6.0.2", "tailwindcss": "^4.0.7", "vite": "^7.1.4" }, "dependencies": { "react": "^19.1.1", "react-dom": "^19.1.1" } } ``` ### Current vs. Expected behavior Currently React breaks immediately on page load. So the page stops working. I believe this is due to invalid syntax in the bundled markup. As mentioned in Additional content, bypassing Vites' dependency optimization gets around the error, but I think this is not the ideal/long term solution. ### What version of Better Auth are you using? 1.3.8 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6020", "release": "24.6.0", "cpuCount": 12, "cpuModel": "Apple M2 Pro", "totalMemory": "32.00 GB", "freeMemory": "0.38 GB" }, "node": { "version": "v22.17.1", "env": "development" }, "packageManager": { "name": "npm", "version": "11.5.2" }, "frameworks": null, "databases": null, "betterAuth": { "version": "Unknown", "config": null, "error": "ENOENT: no such file or directory, open 'package.json'" } } ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript not included as I believe this is a vite compiling issue ``` ### Additional context I have managed to bypass the issue, by adding to exclude optimizing better-auth to my Vite (Astro, in this case) config: ``` vite: { server: { https: true }, optimizeDeps: { exclude: ["better-auth"], }, }, ```
GiteaMirror added the astro label 2026-03-13 09:08:07 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 5, 2025):

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

This is a known issue with Vite and better-auth: Vite's dependency optimization can include server-only or untranspiled ES6+ code (like the super keyword) from better-auth in the client bundle, which causes the syntax error you’re seeing. The recommended fix is to exclude better-auth from Vite's dependency optimization by adding optimizeDeps.exclude: ['better-auth'] to your Vite config, which you’ve already done (source).

For a more robust solution, make sure you are importing from the explicit client entrypoint: use import { ... } from 'better-auth/client' in your client-side code. This ensures only browser-safe code is bundled and avoids pulling in server-only modules (source).

These steps are the officially recommended approach for Vite projects using better-auth.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Sep 5, 2025): <!-- Greeting --> Hi @nebrekab! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a known issue with Vite and better-auth: Vite's dependency optimization can include server-only or untranspiled ES6+ code (like the `super` keyword) from better-auth in the client bundle, which causes the syntax error you’re seeing. The recommended fix is to exclude `better-auth` from Vite's dependency optimization by adding `optimizeDeps.exclude: ['better-auth']` to your Vite config, which you’ve already done ([source](https://github.com/better-auth/better-auth/issues/4082)). For a more robust solution, make sure you are importing from the explicit client entrypoint: use `import { ... } from 'better-auth/client'` in your client-side code. This ensures only browser-safe code is bundled and avoids pulling in server-only modules ([source](https://github.com/better-auth/better-auth/issues/3498)). These steps are the officially recommended approach for Vite projects using better-auth. <!-- 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/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e3873c42-9a64-4c21-9c18-3dc5a4a7cd51?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&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/4457)
Author
Owner

@gvkhna commented on GitHub (Sep 5, 2025):

I traced the error, although strange this git blame is from last year but in my case it's breaking at better-call.

This should be a type-only import

9833a7edee/packages/better-auth/src/plugins/organization/organization.ts (L1)

Rolling back to 1.3.7 has no error so it's some change in the last version release.

@gvkhna commented on GitHub (Sep 5, 2025): I traced the error, although strange this git blame is from last year but in my case it's breaking at better-call. This should be a type-only import https://github.com/better-auth/better-auth/blob/9833a7edeef38e6d287badc4b8d11fe598ad55be/packages/better-auth/src/plugins/organization/organization.ts#L1 Rolling back to 1.3.7 has no error so it's some change in the last version release.
Author
Owner

@Kinfe123 commented on GitHub (Sep 5, 2025):

The issue seems more of the vite optimization like how they try to prebundle and mangle the super keyword usage to somehow lost context. ref - https://vite.dev/guide/dep-pre-bundling.html also here the better-call uses super keyword. for now ig it is better to exclude better-auth from optimizations.

@Kinfe123 commented on GitHub (Sep 5, 2025): The issue seems more of the vite optimization like how they try to prebundle and mangle the super keyword usage to somehow lost context. ref - https://vite.dev/guide/dep-pre-bundling.html also here the `better-call` uses super keyword. for now ig it is better to exclude better-auth from optimizations.
Author
Owner

@gvkhna commented on GitHub (Sep 5, 2025):

The issue seems more of the vite optimization like how they try to prebundle and mangle the super keyword usage to somehow lost context. ref - https://vite.dev/guide/dep-pre-bundling.html also here the better-call uses super keyword. for now ig it is better to exclude better-auth from optimizations.

Uh wut? better-call is a web server framework, it shouldn’t be imported by the client. I highlighted the line that is causing the import, if you do type only import, the entire module won’t be imported to client code.

Can someone from core team add typescript verbatim imports, for type only imports, this should’ve been caught already.

@gvkhna commented on GitHub (Sep 5, 2025): > The issue seems more of the vite optimization like how they try to prebundle and mangle the super keyword usage to somehow lost context. ref - https://vite.dev/guide/dep-pre-bundling.html also here the `better-call` uses super keyword. for now ig it is better to exclude better-auth from optimizations. Uh wut? better-call is a web server framework, it shouldn’t be imported by the client. I highlighted the line that is causing the import, if you do type only import, the entire module won’t be imported to client code. Can someone from core team add typescript verbatim imports, for type only imports, this should’ve been caught already.
Author
Owner

@margaretjoanmiller commented on GitHub (Sep 5, 2025):

Hitting this too. This shouldn't need a workaround or this should be better documented to be honest.

@margaretjoanmiller commented on GitHub (Sep 5, 2025): Hitting this too. This shouldn't need a workaround or this should be better documented to be honest.
Author
Owner

@trupu commented on GitHub (Sep 5, 2025):

Facing the same issue here on ^1.3.6/1.3.8

optimizeDeps: {
   exclude: ['better-auth']
}

works fine for a workaround

@trupu commented on GitHub (Sep 5, 2025): Facing the same issue here on ^1.3.6/1.3.8 ``` optimizeDeps: { exclude: ['better-auth'] } ``` works fine for a workaround
Author
Owner

@Kinfe123 commented on GitHub (Sep 5, 2025):

@trupu @margaretjoanmiller if you can do minimal repro for this. that would be good. ig it should be compatible with dep optimization.

@Kinfe123 commented on GitHub (Sep 5, 2025): @trupu @margaretjoanmiller if you can do minimal repro for this. that would be good. ig it should be compatible with dep optimization.
Author
Owner

@erquhart commented on GitHub (Sep 7, 2025):

@Kinfe123 basic repro here: https://github.com/erquhart/better-auth-issue-4457-repro

It's a basic TanStack Start quickstart, only src/routes/index.tsx sets up an authClient with a single plugin, which triggers the error.

@erquhart commented on GitHub (Sep 7, 2025): @Kinfe123 basic repro here: https://github.com/erquhart/better-auth-issue-4457-repro It's a basic TanStack Start quickstart, only src/routes/index.tsx sets up an authClient with a single plugin, which triggers the error.
Author
Owner

@vivek700 commented on GitHub (Sep 7, 2025):

Facing the same issue in Svelte: ^5.38.3 with better-auth: ^1.3.8

optimizeDeps: { exclude: ["better-auth"] }

Adding better-auth to the optimizeDeps.Excluding the array in my vite.config.js file resolves the issue.

@vivek700 commented on GitHub (Sep 7, 2025): Facing the same issue in Svelte: ^5.38.3 with better-auth: ^1.3.8 ` optimizeDeps: { exclude: ["better-auth"] } ` Adding better-auth to the optimizeDeps.Excluding the array in my vite.config.js file resolves the issue.
Author
Owner

@himself65 commented on GitHub (Sep 7, 2025):

We fixed this in 665bc84ede and I think the core reason is vite transpile code incorrectly, so maybe you set the target to ES2022 will fix this

@himself65 commented on GitHub (Sep 7, 2025): We fixed this in https://github.com/better-auth/better-auth/commit/665bc84ede80fd69e6f2361db92dc665ae8ea3e6 and I think the core reason is vite transpile code incorrectly, so maybe you set the target to `ES2022` will fix this
Author
Owner

@erquhart commented on GitHub (Sep 7, 2025):

I'm actually finding this only happens with outdated versions of Vite, doesn't happen with latest.

@erquhart commented on GitHub (Sep 7, 2025): I'm actually finding this only happens with outdated versions of Vite, doesn't happen with latest.
Author
Owner

@khantseithu commented on GitHub (Sep 8, 2025):

+1

@khantseithu commented on GitHub (Sep 8, 2025): +1
Author
Owner

@nebrekab commented on GitHub (Sep 8, 2025):

Apologies, but my test case was not configured properly and this issue does still seem to present itself... Still an issue for me with Vite 7.1.5.

The optimizeDeps workaround is still the way to go for now.

@himself65 - I tried setting Vite's compilerOptions (and build) target to "ES2022", but it still presented the issue.

@nebrekab commented on GitHub (Sep 8, 2025): Apologies, but my test case was not configured properly and this issue does still seem to present itself... Still an issue for me with Vite 7.1.5. The _optimizeDeps_ workaround is still the way to go for now. @himself65 - I tried setting Vite's compilerOptions (and build) target to "ES2022", but it still presented the issue.
Author
Owner

@himself65 commented on GitHub (Sep 8, 2025):

Please try 1.3.9. I believe we have fixed that!

@himself65 commented on GitHub (Sep 8, 2025): Please try 1.3.9. I believe we have fixed that!
Author
Owner

@himself65 commented on GitHub (Sep 8, 2025):

Fixed in 665bc84ede

@himself65 commented on GitHub (Sep 8, 2025): Fixed in https://github.com/better-auth/better-auth/commit/665bc84ede80fd69e6f2361db92dc665ae8ea3e6
Author
Owner

@nebrekab commented on GitHub (Sep 8, 2025):

Yes, @himself65 , that is indeed fixed with the 1.3.9 better-auth update. Now no need for optimizeDeps work around.

Thanks very much 🚀

@nebrekab commented on GitHub (Sep 8, 2025): Yes, @himself65 , that is indeed fixed with the 1.3.9 better-auth update. Now no need for _optimizeDeps_ work around. Thanks very much 🚀
Author
Owner

@jornverhoeven commented on GitHub (Sep 8, 2025):

@himself65 The 1.3.9 version seems to have fixed the problem indeed. Amazing work, thanks a million :)

@jornverhoeven commented on GitHub (Sep 8, 2025): @himself65 The 1.3.9 version seems to have fixed the problem indeed. Amazing work, thanks a million :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1851