[GH-ISSUE #6580] Svetelkit app fails to run after build #27882

Closed
opened 2026-04-17 19:08:39 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @ksKao on GitHub (Dec 7, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6580

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a brand new Sveltekit project by running npx sv create test-svelte (minimal template, use typescript, no additional libraries, npm package manager)
  2. Setup Better Auth according to docs. (with pg driver, no ORM)
  3. Add Session and User types to Locals interface in app.d.ts
  4. Run npm i -D @sveltejs/adapter-node
  5. Update svelte.config.js to use the adapter-node by updating the import statement on top
  6. Run npm run build
  7. Run node build

Current vs. Expected behavior

The server should start up as expected, but immediately crashes with the following error:

Listening on http://0.0.0.0:3000
file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:32734
  const sessionToken = createCookie("session_token", { maxAge: options.session?.expiresIn || msExports.ms("7d") / 1e3 });
                                                                                                       ^

TypeError: msExports.ms is not a function
    at getCookies (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:32734:104)
    at createAuthContext (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:36414:19)
    at init (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:36549:21)

Node.js v24.11.1

This is the only dependency that I have installed (other than the pg driver), and setting all these up I tested with a fresh init of the Sveltekit minimal template (it can be built and run with the same commands without issue)

What version of Better Auth are you using?

1.4.5

System info

{
  "system": {
    "platform": "linux",
    "arch": "x64",
    "version": "#1 SMP PREEMPT_DYNAMIC Mon, 24 Nov 2025 15:21:09 +0000",
    "release": "6.17.9-arch1-1",
    "cpuCount": 16,
    "cpuModel": "AMD Ryzen 7 9800X3D 8-Core Processor",
    "totalMemory": "60.43 GB",
    "freeMemory": "50.14 GB"
  },
  "node": {
    "version": "v24.11.1",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.2"
  },
  "frameworks": [
    {
      "name": "svelte",
      "version": "^5.43.8"
    },
    {
      "name": "@sveltejs/kit",
      "version": "^2.48.5"
    }
  ],
  "databases": [
    {
      "name": "pg",
      "version": "^8.16.3"
    }
  ],
  "betterAuth": {
    "version": "^1.4.5",
    "config": {
      "database": {
        "_events": {},
        "_eventsCount": 0,
        "options": {
          "connectionString": "[REDACTED]",
          "max": 10,
          "min": 0,
          "maxUses": null,
          "allowExitOnIdle": false,
          "maxLifetimeSeconds": 0,
          "idleTimeoutMillis": 10000
        },
        "_clients": [],
        "_idle": [],
        "_expired": {},
        "_pendingQueue": [],
        "ending": false,
        "ended": false
      },
      "emailAndPassword": {
        "enabled": true
      },
      "plugins": [
        {
          "name": "sveltekit-cookies",
          "config": {
            "id": "sveltekit-cookies",
            "hooks": {
              "after": [
                {}
              ]
            }
          }
        }
      ]
    }
  }
}

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

Other

Auth config (if applicable)

import { getRequestEvent } from "$app/server";
import { betterAuth } from "better-auth";
import { sveltekitCookies } from "better-auth/svelte-kit";
import { Pool } from "pg";

export const auth = betterAuth({
  database: new Pool({
    connectionString: "postgres://postgres:1234567@localhost:5432",
  }),
    //...other options
  emailAndPassword: { 
    enabled: true, 
  }, 
  plugins: [sveltekitCookies(getRequestEvent)], // make sure this is the last plugin in the array
});

Additional context

Also tried with the Vercel adapter (and deploying to vercel), a similar error (not exactly the same) occurs.

Originally created by @ksKao on GitHub (Dec 7, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6580 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a brand new Sveltekit project by running `npx sv create test-svelte` (minimal template, use typescript, no additional libraries, npm package manager) 2. Setup Better Auth according to docs. (with pg driver, no ORM) 3. Add `Session` and `User` types to `Locals` interface in `app.d.ts` 4. Run `npm i -D @sveltejs/adapter-node` 5. Update `svelte.config.js` to use the `adapter-node` by updating the import statement on top 6. Run `npm run build` 7. Run `node build` ### Current vs. Expected behavior The server should start up as expected, but immediately crashes with the following error: ``` Listening on http://0.0.0.0:3000 file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:32734 const sessionToken = createCookie("session_token", { maxAge: options.session?.expiresIn || msExports.ms("7d") / 1e3 }); ^ TypeError: msExports.ms is not a function at getCookies (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:32734:104) at createAuthContext (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:36414:19) at init (file:///home/user/Repos/test-svelte/build/server/chunks/hooks.server-BVCvbRJl.js:36549:21) Node.js v24.11.1 ``` This is the only dependency that I have installed (other than the pg driver), and setting all these up I tested with a fresh init of the Sveltekit minimal template (it can be built and run with the same commands without issue) ### What version of Better Auth are you using? 1.4.5 ### System info ```bash { "system": { "platform": "linux", "arch": "x64", "version": "#1 SMP PREEMPT_DYNAMIC Mon, 24 Nov 2025 15:21:09 +0000", "release": "6.17.9-arch1-1", "cpuCount": 16, "cpuModel": "AMD Ryzen 7 9800X3D 8-Core Processor", "totalMemory": "60.43 GB", "freeMemory": "50.14 GB" }, "node": { "version": "v24.11.1", "env": "development" }, "packageManager": { "name": "npm", "version": "11.6.2" }, "frameworks": [ { "name": "svelte", "version": "^5.43.8" }, { "name": "@sveltejs/kit", "version": "^2.48.5" } ], "databases": [ { "name": "pg", "version": "^8.16.3" } ], "betterAuth": { "version": "^1.4.5", "config": { "database": { "_events": {}, "_eventsCount": 0, "options": { "connectionString": "[REDACTED]", "max": 10, "min": 0, "maxUses": null, "allowExitOnIdle": false, "maxLifetimeSeconds": 0, "idleTimeoutMillis": 10000 }, "_clients": [], "_idle": [], "_expired": {}, "_pendingQueue": [], "ending": false, "ended": false }, "emailAndPassword": { "enabled": true }, "plugins": [ { "name": "sveltekit-cookies", "config": { "id": "sveltekit-cookies", "hooks": { "after": [ {} ] } } } ] } } } ``` ### Which area(s) are affected? (Select all that apply) Other ### Auth config (if applicable) ```typescript import { getRequestEvent } from "$app/server"; import { betterAuth } from "better-auth"; import { sveltekitCookies } from "better-auth/svelte-kit"; import { Pool } from "pg"; export const auth = betterAuth({ database: new Pool({ connectionString: "postgres://postgres:1234567@localhost:5432", }), //...other options emailAndPassword: { enabled: true, }, plugins: [sveltekitCookies(getRequestEvent)], // make sure this is the last plugin in the array }); ``` ### Additional context Also tried with the Vercel adapter (and deploying to vercel), a similar error (not exactly the same) occurs.
GiteaMirror added the locked label 2026-04-17 19:08:39 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 7, 2025):

Thanks for reporting this issue! The error “msExports.ms is not a function” indicates that the ms package (used to help calculate cookie expiration) isn’t being invoked as expected. This may be caused by a dependency version conflict or an unexpected change in the ms package export. Here are a few steps to troubleshoot:

• Verify that you haven’t inadvertently installed an incompatible version of the ms package. Sometimes a dependency update or resolution can alter the expected export. If you have ms as a direct dependency, try removing it so that better-auth’s internal dependency takes precedence.

• Delete your node_modules and reinstall all dependencies. This can clear up any mismatches in the dependency tree that might cause ms to be imported incorrectly.

• Ensure your auth configuration adheres to the recommended usage—note that the sveltekit-cookies plugin must be last in the plugins array, as suggested in our docs [1]. Although your configuration appears correct, double-check there isn’t another plugin or custom override affecting cookie creation.

Have you recently updated any packages in your project or noticed any other dependency conflicts? Tag @better-auth-agent if you would like an answer from the Agent.

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3621789553 --> @better-auth-agent[bot] commented on GitHub (Dec 7, 2025): Thanks for reporting this issue! The error “msExports.ms is not a function” indicates that the ms package (used to help calculate cookie expiration) isn’t being invoked as expected. This may be caused by a dependency version conflict or an unexpected change in the ms package export. Here are a few steps to troubleshoot: • Verify that you haven’t inadvertently installed an incompatible version of the ms package. Sometimes a dependency update or resolution can alter the expected export. If you have ms as a direct dependency, try removing it so that better-auth’s internal dependency takes precedence. • Delete your node_modules and reinstall all dependencies. This can clear up any mismatches in the dependency tree that might cause ms to be imported incorrectly. • Ensure your auth configuration adheres to the recommended usage—note that the sveltekit-cookies plugin must be last in the plugins array, as suggested in our docs [[1]](https://github.com/better-auth/better-auth/issues/6446). Although your configuration appears correct, double-check there isn’t another plugin or custom override affecting cookie creation. Have you recently updated any packages in your project or noticed any other dependency conflicts? Tag @better-auth-agent if you would like an answer from the Agent. _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@dosubot[bot] commented on GitHub (Dec 7, 2025):

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

This error happens because Better Auth imports the ms package using a named import (import { ms } from "ms"), but the actual ms package exports a default function. When SvelteKit builds your app, the bundler rewrites the import and ends up calling a property that doesn't exist, causing the crash at runtime (source).

Recent updates to Better Auth (December 2025 and later) include changes to dependency management and may fix this issue (source). If you can, try upgrading Better Auth and the ms package to the latest versions.

If you need a quick workaround on v1.4.5, you can patch the import in your local node_modules/better-auth/src/cookies/index.js (or the built file) from:

import { ms } from "ms";

to:

import ms from "ms";

and update all usages to call ms(...) directly.

Alternatively, you can use a package like patch-package to automate this fix until an official update is released.

This issue is specific to production builds—development mode usually works because the import style isn't transformed by the bundler. If you want to confirm, try running your app in dev mode (npm run dev) and see if it starts up without crashing.

Let me know if you need more details on patching or upgrading!

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:3621790707 --> @dosubot[bot] commented on GitHub (Dec 7, 2025): <!-- Greeting --> Hi @ksKao! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This error happens because Better Auth imports the `ms` package using a named import (`import { ms } from "ms"`), but the actual `ms` package exports a default function. When SvelteKit builds your app, the bundler rewrites the import and ends up calling a property that doesn't exist, causing the crash at runtime ([source](https://github.com/better-auth/better-auth/blob/dbe2a9aea380f19804843f6a8c76cf123b3080a6/packages/better-auth/src/cookies/index.ts#L13-L111)). Recent updates to Better Auth (December 2025 and later) include changes to dependency management and may fix this issue ([source](https://github.com/better-auth/better-auth/pull/6309)). If you can, try upgrading Better Auth and the `ms` package to the latest versions. If you need a quick workaround on v1.4.5, you can patch the import in your local `node_modules/better-auth/src/cookies/index.js` (or the built file) from: ```js import { ms } from "ms"; ``` to: ```js import ms from "ms"; ``` and update all usages to call `ms(...)` directly. Alternatively, you can use a package like `patch-package` to automate this fix until an official update is released. This issue is specific to production builds—development mode usually works because the import style isn't transformed by the bundler. If you want to confirm, try running your app in dev mode (`npm run dev`) and see if it starts up without crashing. Let me know if you need more details on patching or upgrading! <!-- 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/a7257845-e443-4327-8a98-3d798b649512?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/a7257845-e443-4327-8a98-3d798b649512?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%20error%20happens%20because%20Better%20Auth%20imports%20the%20%60ms%60%20package%20using%20a%20named%20import%20%28%60import%20%7B%20ms%20%7D%20from%20%22ms%22%60%29%2C%20but%20the%20actual%20%60ms%60%20package%20exports%20a%20default%20function.%20When%20SvelteKit%20builds%20your%20app%2C%20the%20bundler%20rewrites%20the%20import%20and%20ends%20up%20calling%20a%20property%20that%20doesn%27t%20exist%2C%20causing%20the%20crash%20at%20runtime%20%28%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/blob/dbe2a9aea380f19804843f6a8c76cf123b3080a6/packages/better-auth/src/cookies/index.ts%23L13-L111%29%29.%0A%0ARecent%20updates%20to%20Better%20Auth%20%28December%202025%20and%20later%29%20include%20changes%20to%20dependency%20management%20and%20may%20fix%20this%20issue%20%28%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/pull/6309%29%29.%20If%20you%20can%2C%20try%20upgrading%20Better%20Auth%20and%20the%20%60ms%60%20package%20to%20the%20latest%20versions.%0A%0AIf%20you%20need%20a%20quick%20workaround%20on%20v1.4.5%2C%20you%20can%20patch%20the%20import%20in%20your%20local%20%60node_modules/better-auth/src/cookies/index.js%60%20%28or%20the%20built%20file%29%20from%3A%0A%60%60%60js%0Aimport%20%7B%20ms%20%7D%20from%20%22ms%22%3B%0A%60%60%60%0Ato%3A%0A%60%60%60js%0Aimport%20ms%20from%20%22ms%22%3B%0A%60%60%60%0Aand%20update%20all%20usages%20to%20call%20%60ms%28...%29%60%20directly.%0A%0AAlternatively%2C%20you%20can%20use%20a%20package%20like%20%60patch-package%60%20to%20automate%20this%20fix%20until%20an%20official%20update%20is%20released.%0A%0AThis%20issue%20is%20specific%20to%20production%20builds%E2%80%94development%20mode%20usually%20works%20because%20the%20import%20style%20isn%27t%20transformed%20by%20the%20bundler.%20If%20you%20want%20to%20confirm%2C%20try%20running%20your%20app%20in%20dev%20mode%20%28%60npm%20run%20dev%60%29%20and%20see%20if%20it%20starts%20up%20without%20crashing.%0A%0ALet%20me%20know%20if%20you%20need%20more%20details%20on%20patching%20or%20upgrading%21)&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/6580)
Author
Owner

@ksKao commented on GitHub (Dec 7, 2025):

Downgraded to version 1.4.4 and it works. Still unsure about the root cause.

<!-- gh-comment-id:3621963479 --> @ksKao commented on GitHub (Dec 7, 2025): Downgraded to version 1.4.4 and it works. Still unsure about the root cause.
Author
Owner

@ismailinterkod commented on GitHub (Dec 7, 2025):

Downgraded to version 1.4.4 and it works. Still unsure about the root cause.

thanks, its works.

<!-- gh-comment-id:3622064381 --> @ismailinterkod commented on GitHub (Dec 7, 2025): > Downgraded to version 1.4.4 and it works. Still unsure about the root cause. thanks, its works.
Author
Owner

@bytaesu commented on GitHub (Dec 9, 2025):

Hello @ksKao @ismailinterkod,

I’ve looked into this issue, and it seems the bundling problem started after ms was moved from devDependencies to dependencies.

Since we don’t rely on ms much, we plan to remove it soon, and the issue should disappear after that. We could apply a quick patch now, but we’d prefer to address the root cause. Thanks for your patience 🙏

<!-- gh-comment-id:3630059168 --> @bytaesu commented on GitHub (Dec 9, 2025): Hello @ksKao @ismailinterkod, I’ve looked into this issue, and it seems the bundling problem started after `ms` was moved from devDependencies to dependencies. Since we don’t rely on `ms` much, we plan to remove it soon, and the issue should disappear after that. We could apply a quick patch now, but we’d prefer to address the root cause. Thanks for your patience 🙏
Author
Owner

@ksKao commented on GitHub (Dec 9, 2025):

Thanks for the reply. Noted on that.

Really appreciate the awesome work you guys have put into the library. 😀

<!-- gh-comment-id:3630647142 --> @ksKao commented on GitHub (Dec 9, 2025): Thanks for the reply. Noted on that. Really appreciate the awesome work you guys have put into the library. 😀
Author
Owner

@bytaesu commented on GitHub (Dec 11, 2025):

Hello @ksKao,

This issue will be resolved in the next release 🙂
https://github.com/better-auth/better-auth/pull/6686

<!-- gh-comment-id:3640333284 --> @bytaesu commented on GitHub (Dec 11, 2025): Hello @ksKao, This issue will be resolved in the next release 🙂 https://github.com/better-auth/better-auth/pull/6686
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#27882