[GH-ISSUE #8382] createAuthMiddleware issue. It is not exported in node_modules/better-auth/dist/plugins/index.mjs (?) #19705

Closed
opened 2026-04-15 19:00:28 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @bbgrk on GitHub (Mar 4, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8382

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  • I am running Astro v5 (probably does not matter, see below)
  • In better-auth v1.5.3 this the error message running astro run dev
17:00:49 [ERROR] [MiddlewareCantBeLoaded] An unknown error was thrown while loading your middleware.
  Error reference:
    https://docs.astro.build/en/reference/errors/middleware-cant-be-loaded/
  Stack trace:
    at /root/repositories/productlionstorefront/node_modules/astro/dist/core/middleware/loadMiddleware.js:8:28
    [...] See full stack trace in the browser, or rerun with --verbose.
  Caused by:
  Export named 'createAuthMiddleware' not found in module '/root/repositories/productlionstorefront/node_modules/better-auth/dist/plugins/index.mjs'.
    at <parse> (:0)
    at linkAndEvaluateModule (native:1:11)
    at processTicksAndRejections (native:7:39)
  • I am trying to use a hook in auth.ts.
  • Documentation says the same.
  • Example:
    hooks: {
        // https://www.better-auth.com/docs/reference/options#hooks
        after: createAuthMiddleware(async (ctx) => {
            //console.log(ctx);
        }),
    },
  • Now in better-auth v1.4.21 the createAuthMiddleware is exported properly and it works fine:
import { createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from "@better-auth/core/api";
  • In better-auth v1.5.3 createAuthMiddleware is not exported.
  • If I manually change the node_modules/better-auth/dist/plugins/index.mjs by adding this into index.mjs:
import { createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from "@better-auth/core/api";

//
// and I export it as (v1.4.21 has it) like: 
//

export { MULTI_SESSION_ERROR_CODES as ERROR_CODES, HIDE_METADATA, TWO_FACTOR_ERROR_CODES, USERNAME_ERROR_CODES, createAuthEndpoint, createAuthMiddleware, optionsMiddleware, admin, anonymous, auth0, bearer, captcha, createAccessControl, createJwk, customSession, deviceAuthorization, deviceAuthorizationOptionsSchema, emailOTP, generateExportedKeyPair, genericOAuth, getClient, getJwtToken, getMCPProtectedResourceMetadata, getMCPProviderMetadata, getMetadata, getOrgAdapter, gumroad, hasPermission, haveIBeenPwned, hubspot, jwt, keycloak, lastLoginMethod, line, magicLink, mcp, microsoftEntraId, multiSession, oAuthDiscoveryMetadata, oAuthProtectedResourceMetadata, oAuthProxy, oidcProvider, okta, oneTap, oneTimeToken, openAPI, organization, parseRoles, patreon, phoneNumber, role, signJWT, siwe, slack, testUtils, toExpJWT, twoFactor, twoFactorClient, username, verifyJWT, withMcpAuth };
  • It works just fine.

  • So I read the 1.5.0 tag release notes. There were some changes. But I do not see any difference in importing/using createAuthMiddleware.

  • What am I missing?

Thanks in advance.

Current vs. Expected behavior

Current: createAuthMiddleware is unusable on v1.5.3.
Expected: Having createAuthMiddleware exported and usable.

What version of Better Auth are you using?

1.5.3

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#12-Ubuntu SMP PREEMPT_DYNAMIC Fri Jan  9 20:46:52 UTC 2026",
    "release": "6.17.0-12-generic",
    "cpuCount": 16,
    "cpuModel": "Intel(R) Xeon(R) Silver 4112 CPU @ 2.60GHz",
    "totalMemory": "61.14 GB",
    "freeMemory": "9.08 GB"
  },
  "node": {
    "version": "v24.3.0",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.3.10"
  },
  "frameworks": [
    {
      "name": "react",
      "version": "^19.2.4"
    },
    {
      "name": "astro",
      "version": "^5.18.0"
    }
  ],
  "databases": [
    {
      "name": "@libsql/client",
      "version": "^0.17.0"
    },
    {
      "name": "drizzle",
      "version": "^0.45.1"
    }
  ],
  "betterAuth": {
    "version": "Unknown",
    "config": null,
    "error": "process.env.DB_CONFIG_URL is undefined"
  }
}

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

Client, Backend, Types, Documentation, Package

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @bbgrk on GitHub (Mar 4, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8382 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce - I am running Astro v5 (probably does not matter, see below) - In better-auth v1.5.3 this the error message running `astro run dev` ```ts 17:00:49 [ERROR] [MiddlewareCantBeLoaded] An unknown error was thrown while loading your middleware. Error reference: https://docs.astro.build/en/reference/errors/middleware-cant-be-loaded/ Stack trace: at /root/repositories/productlionstorefront/node_modules/astro/dist/core/middleware/loadMiddleware.js:8:28 [...] See full stack trace in the browser, or rerun with --verbose. Caused by: Export named 'createAuthMiddleware' not found in module '/root/repositories/productlionstorefront/node_modules/better-auth/dist/plugins/index.mjs'. at <parse> (:0) at linkAndEvaluateModule (native:1:11) at processTicksAndRejections (native:7:39) ``` - I am trying to use a hook in `auth.ts`. - Documentation says the same. - Example: ```ts hooks: { // https://www.better-auth.com/docs/reference/options#hooks after: createAuthMiddleware(async (ctx) => { //console.log(ctx); }), }, ``` - Now in better-auth v1.4.21 the `createAuthMiddleware` is exported properly and it works fine: ```ts import { createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from "@better-auth/core/api"; ``` - In better-auth v1.5.3 `createAuthMiddleware` is not exported. - If I manually change the `node_modules/better-auth/dist/plugins/index.mjs` by adding this into `index.mjs`: ```ts import { createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from "@better-auth/core/api"; // // and I export it as (v1.4.21 has it) like: // export { MULTI_SESSION_ERROR_CODES as ERROR_CODES, HIDE_METADATA, TWO_FACTOR_ERROR_CODES, USERNAME_ERROR_CODES, createAuthEndpoint, createAuthMiddleware, optionsMiddleware, admin, anonymous, auth0, bearer, captcha, createAccessControl, createJwk, customSession, deviceAuthorization, deviceAuthorizationOptionsSchema, emailOTP, generateExportedKeyPair, genericOAuth, getClient, getJwtToken, getMCPProtectedResourceMetadata, getMCPProviderMetadata, getMetadata, getOrgAdapter, gumroad, hasPermission, haveIBeenPwned, hubspot, jwt, keycloak, lastLoginMethod, line, magicLink, mcp, microsoftEntraId, multiSession, oAuthDiscoveryMetadata, oAuthProtectedResourceMetadata, oAuthProxy, oidcProvider, okta, oneTap, oneTimeToken, openAPI, organization, parseRoles, patreon, phoneNumber, role, signJWT, siwe, slack, testUtils, toExpJWT, twoFactor, twoFactorClient, username, verifyJWT, withMcpAuth }; ``` - It works just fine. - So I read the 1.5.0 tag release notes. There were some changes. But I do not see any difference in importing/using `createAuthMiddleware`. - What am I missing? Thanks in advance. ### Current vs. Expected behavior Current: `createAuthMiddleware` is unusable on v1.5.3. Expected: Having `createAuthMiddleware` exported and usable. ### What version of Better Auth are you using? 1.5.3 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#12-Ubuntu SMP PREEMPT_DYNAMIC Fri Jan 9 20:46:52 UTC 2026", "release": "6.17.0-12-generic", "cpuCount": 16, "cpuModel": "Intel(R) Xeon(R) Silver 4112 CPU @ 2.60GHz", "totalMemory": "61.14 GB", "freeMemory": "9.08 GB" }, "node": { "version": "v24.3.0", "env": "development" }, "packageManager": { "name": "bun", "version": "1.3.10" }, "frameworks": [ { "name": "react", "version": "^19.2.4" }, { "name": "astro", "version": "^5.18.0" } ], "databases": [ { "name": "@libsql/client", "version": "^0.17.0" }, { "name": "drizzle", "version": "^0.45.1" } ], "betterAuth": { "version": "Unknown", "config": null, "error": "process.env.DB_CONFIG_URL is undefined" } } ``` ### Which area(s) are affected? (Select all that apply) Client, Backend, Types, Documentation, Package ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-15 19:00:28 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Mar 4, 2026):

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

This is a breaking change in v1.5.x. The createAuthMiddleware was removed from better-auth/plugins in January 2026 as part of a deprecated API cleanup.

The correct import path for v1.5.3 is:

import { createAuthMiddleware } from "better-auth/api";

Your hooks configuration should then work:

import { createAuthMiddleware } from "better-auth/api";

export const auth = betterAuth({
  hooks: {
    after: createAuthMiddleware(async (ctx) => {
      //console.log(ctx);
    }),
  },
});

The function is exported from packages/better-auth/src/api/index.ts and better-auth/api is an officially supported export path in the package.json.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3999697121 --> @dosubot[bot] commented on GitHub (Mar 4, 2026): <!-- Greeting --> Hi @DRN88! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This is a breaking change in v1.5.x. The `createAuthMiddleware` was [removed from `better-auth/plugins`](https://github.com/better-auth/better-auth/pull/7623) in January 2026 as part of a deprecated API cleanup. **The correct import path for v1.5.3 is:** ```typescript import { createAuthMiddleware } from "better-auth/api"; ``` Your hooks configuration should then work: ```typescript import { createAuthMiddleware } from "better-auth/api"; export const auth = betterAuth({ hooks: { after: createAuthMiddleware(async (ctx) => { //console.log(ctx); }), }, }); ``` The function is [exported from `packages/better-auth/src/api/index.ts`](https://github.com/better-auth/better-auth/blob/8e1ddc3f81c11c2fff67e799edc2d742742f0603/packages/better-auth/src/api/index.ts#L373-L379) and `better-auth/api` is [an officially supported export path](https://github.com/better-auth/better-auth/blob/8e1ddc3f81c11c2fff67e799edc2d742742f0603/packages/better-auth/package.json#L108-L112) in the 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/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/56a9075e-ddff-4e5c-b879-ecea083d4c31?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20is%20a%20breaking%20change%20in%20v1.5.x.%20The%20%60createAuthMiddleware%60%20was%20%5Bremoved%20from%20%60better-auth/plugins%60%5D%28https%3A//github.com/better-auth/better-auth/pull/7623%29%20in%20January%202026%20as%20part%20of%20a%20deprecated%20API%20cleanup.%0A%0A%2A%2AThe%20correct%20import%20path%20for%20v1.5.3%20is%3A%2A%2A%0A%60%60%60typescript%0Aimport%20%7B%20createAuthMiddleware%20%7D%20from%20%22better-auth/api%22%3B%0A%60%60%60%0A%0AYour%20hooks%20configuration%20should%20then%20work%3A%0A%60%60%60typescript%0Aimport%20%7B%20createAuthMiddleware%20%7D%20from%20%22better-auth/api%22%3B%0A%0Aexport%20const%20auth%20%3D%20betterAuth%28%7B%0A%20%20hooks%3A%20%7B%0A%20%20%20%20after%3A%20createAuthMiddleware%28async%20%28ctx%29%20%3D%3E%20%7B%0A%20%20%20%20%20%20//console.log%28ctx%29%3B%0A%20%20%20%20%7D%29%2C%0A%20%20%7D%2C%0A%7D%29%3B%0A%60%60%60%0A%0AThe%20function%20is%20%5Bexported%20from%20%60packages/better-auth/src/api/index.ts%60%5D%28https%3A//github.com/better-auth/better-auth/blob/8e1ddc3f81c11c2fff67e799edc2d742742f0603/packages/better-auth/src/api/index.ts%23L373-L379%29%20and%20%60better-auth/api%60%20is%20%5Ban%20officially%20supported%20export%20path%5D%28https%3A//github.com/better-auth/better-auth/blob/8e1ddc3f81c11c2fff67e799edc2d742742f0603/packages/better-auth/package.json%23L108-L112%29%20in%20the%20package.json.)&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/8382)
Author
Owner

@rmartins0 commented on GitHub (Mar 5, 2026):

Seems like the better auth recent upgrade of a minor version caused a breaking change, violating semver:

Error: Cannot find package '@better-auth/prisma-adapter' imported from /home/runner/work/repo/repo/node_modules/better-auth/dist/adapters/prisma-adapter/index.mjs

P.S. Workaround

Workaround is installing the dependency, as specified in the changes between 1.5.3 and 1.5.1:

To use the Prisma adapter, you need to install the @better-auth/prisma-adapter package:

@better-auth/prisma-adapter

https://github.com/better-auth/better-auth/compare/v1.5.1...v1.5.3

<!-- gh-comment-id:4001150642 --> @rmartins0 commented on GitHub (Mar 5, 2026): Seems like the better auth recent upgrade of a minor version caused a breaking change, violating semver: > Error: Cannot find package '@better-auth/prisma-adapter' imported from /home/runner/work/repo/repo/node_modules/better-auth/dist/adapters/prisma-adapter/index.mjs ## P.S. Workaround Workaround is installing the dependency, as specified in the changes between 1.5.3 and 1.5.1: > To use the Prisma adapter, you need to install the `@better-auth/prisma-adapter` package: > ```package-install > @better-auth/prisma-adapter > ``` https://github.com/better-auth/better-auth/compare/v1.5.1...v1.5.3
Author
Owner

@rmartins0 commented on GitHub (Mar 5, 2026):

@Bekacru @himself65 @NathanColosimo please help out

<!-- gh-comment-id:4001181259 --> @rmartins0 commented on GitHub (Mar 5, 2026): @Bekacru @himself65 @NathanColosimo please help out
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19705