[Runtime] Export APIError doesn't exist in target module #1326

Closed
opened 2026-03-13 08:32:49 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @jpainam on GitHub (Jun 8, 2025).

This happens only at runtime.

Error: ./packages/auth/src/index.ts:4:1
Export APIError doesn't exist in target module
  2 | import type { BetterAuthOptions } from "better-auth";
  3 | import { expo } from "@better-auth/expo";
> 4 | import { APIError, betterAuth } from "better-auth";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5 | import { prismaAdapter } from "better-auth/adapters/prisma";
  6 | import { nextCookies } from "better-auth/next-js";
  7 | import { apiKey, oAuthProxy, username } from "better-auth/plugins";

The export APIError was not found in module [project]/node_modules/better-auth/dist/index.mjs [app-route] (ecmascript) <exports>.
Did you mean to import createLogger?
All exports of the module are statically known (It doesn't have dynamic exports). So it's known statically that the requested export doesn't exist.
    at BuildError (http://localhost:3000/_next/static/chunks/%5Broot-of-the-server%5D__e2c08166._.js:17416:41)
    at react-stack-bottom-frame (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:12388:24)
    at renderWithHooks (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:3402:24)
    at updateFunctionComponent (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:4726:21)
    at beginWork (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:5358:24)
    at runWithFiberInDEV (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:631:20)
    at performUnitOfWork (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7947:97)
    at workLoopSync (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7840:40)
    at renderRootSync (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7823:13)
    at performWorkOnRoot (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7558:211)
    at performWorkOnRootViaSchedulerTask (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:8386:9)
    at MessagePort.performWorkUntilDeadline (http://localhost:3000/_next/static/chunks/node_modules_a51498a5._.js:1486:64)

I'm using it here

/* eslint-disable @typescript-eslint/no-unused-vars */
import type { BetterAuthOptions } from "better-auth";
import { APIError, betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import { apiKey, oAuthProxy, username } from "better-auth/plugins";


/* eslint-disable @typescript-eslint/require-await */
export function initAuth(options: {
  baseUrl: string;
  productionUrl: string;
  secret: string | undefined;
}) {
  const config = {
    database: prismaAdapter(db, {
      provider: "postgresql",
    }),
      deleteUser: {
        enabled: true,
        beforeDelete: async (user, request) => {
          if (user.email.includes("admin")) {
            throw new APIError("BAD_REQUEST", {
              message: "Admin accounts can't be deleted",
            });
          }
        },
      },
    },
    baseURL: options.baseUrl,
    secret: options.secret,
    },
    plugins: [
      username(),
      apiKey({
        enableMetadata: true,
      }),
      nextCookies(),
    ],
  } satisfies BetterAuthOptions;

  return betterAuth(config);
}

export type Auth = ReturnType<typeof initAuth>;
export type Session = Auth["$Infer"]["Session"];
Originally created by @jpainam on GitHub (Jun 8, 2025). This happens only at runtime. ```tsx Error: ./packages/auth/src/index.ts:4:1 Export APIError doesn't exist in target module   2 | import type { BetterAuthOptions } from "better-auth";   3 | import { expo } from "@better-auth/expo"; > 4 | import { APIError, betterAuth } from "better-auth";   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   5 | import { prismaAdapter } from "better-auth/adapters/prisma";   6 | import { nextCookies } from "better-auth/next-js";   7 | import { apiKey, oAuthProxy, username } from "better-auth/plugins"; The export APIError was not found in module [project]/node_modules/better-auth/dist/index.mjs [app-route] (ecmascript) <exports>. Did you mean to import createLogger? All exports of the module are statically known (It doesn't have dynamic exports). So it's known statically that the requested export doesn't exist. at BuildError (http://localhost:3000/_next/static/chunks/%5Broot-of-the-server%5D__e2c08166._.js:17416:41) at react-stack-bottom-frame (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:12388:24) at renderWithHooks (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:3402:24) at updateFunctionComponent (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:4726:21) at beginWork (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:5358:24) at runWithFiberInDEV (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:631:20) at performUnitOfWork (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7947:97) at workLoopSync (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7840:40) at renderRootSync (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7823:13) at performWorkOnRoot (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:7558:211) at performWorkOnRootViaSchedulerTask (http://localhost:3000/_next/static/chunks/node_modules_react-dom_82bb97c6._.js:8386:9) at MessagePort.performWorkUntilDeadline (http://localhost:3000/_next/static/chunks/node_modules_a51498a5._.js:1486:64) ``` I'm using it here ```tsx /* eslint-disable @typescript-eslint/no-unused-vars */ import type { BetterAuthOptions } from "better-auth"; import { APIError, betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; import { nextCookies } from "better-auth/next-js"; import { apiKey, oAuthProxy, username } from "better-auth/plugins"; /* eslint-disable @typescript-eslint/require-await */ export function initAuth(options: { baseUrl: string; productionUrl: string; secret: string | undefined; }) { const config = { database: prismaAdapter(db, { provider: "postgresql", }), deleteUser: { enabled: true, beforeDelete: async (user, request) => { if (user.email.includes("admin")) { throw new APIError("BAD_REQUEST", { message: "Admin accounts can't be deleted", }); } }, }, }, baseURL: options.baseUrl, secret: options.secret, }, plugins: [ username(), apiKey({ enableMetadata: true, }), nextCookies(), ], } satisfies BetterAuthOptions; return betterAuth(config); } export type Auth = ReturnType<typeof initAuth>; export type Session = Auth["$Infer"]["Session"]; ```
Author
Owner

@Kinfe123 commented on GitHub (Jun 9, 2025):

which version are you actually using ?

@Kinfe123 commented on GitHub (Jun 9, 2025): which version are you actually using ?
Author
Owner

@jpainam commented on GitHub (Jun 9, 2025):

which version are you actually using ?

"better-auth": "1.2.8",

@jpainam commented on GitHub (Jun 9, 2025): > which version are you actually using ? `"better-auth": "1.2.8",`
Author
Owner

@diegode-tsx commented on GitHub (Jun 9, 2025):

I was using it in a server action and had the same problem, but importing from the api path fixed it:

import { APIError } from 'better-auth/api';

@diegode-tsx commented on GitHub (Jun 9, 2025): I was using it in a server action and had the same problem, but importing from the api path fixed it: `import { APIError } from 'better-auth/api';`
Author
Owner

@Kinfe123 commented on GitHub (Jun 9, 2025):

yeah import from better-auth/api

@Kinfe123 commented on GitHub (Jun 9, 2025): yeah import from `better-auth/api`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1326