[GH-ISSUE #850] Is Next.js pages router supported? #8466

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

Originally created by @PhillJK on GitHub (Dec 10, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/850

Hi, I like the framework and want to use it my project. I have skimmed the docs, and found that all the examples are using new Next.js app router. I wanted to know if the pages router supported, or can be used (integrated) with the one, or there are some incompatibilities and restrictions?

Originally created by @PhillJK on GitHub (Dec 10, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/850 Hi, I like the framework and want to use it my project. I have skimmed the docs, and found that all the examples are using new Next.js app router. I wanted to know if the pages router supported, or can be used (integrated) with the one, or there are some incompatibilities and restrictions?
GiteaMirror added the locked label 2026-04-13 03:32:32 -05:00
Author
Owner

@Bekacru commented on GitHub (Dec 12, 2024):

Since next page routers is express compatible, you can use the node adapter. So It should be supported!

<!-- gh-comment-id:2537953533 --> @Bekacru commented on GitHub (Dec 12, 2024): Since next page routers is express compatible, you can use the node adapter. So It should be supported!
Author
Owner

@PhillJK commented on GitHub (Dec 12, 2024):

Since next page routers is express compatible, you can use the node adapter. So It should be supported!

Oh, I missed that the framework also supports node/express. Thank you, for your clarification and reply! I will look into it.

<!-- gh-comment-id:2538548169 --> @PhillJK commented on GitHub (Dec 12, 2024): > Since next page routers is express compatible, you can use the node adapter. So It should be supported! Oh, I missed that the framework also supports node/express. Thank you, for your clarification and reply! I will look into it.
Author
Owner

@drshika commented on GitHub (Dec 20, 2024):

Hello! I think I might be missing something here. I ran into this issue:

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported.

I tried adding in the Node Adapter as mentioned earlier to src/pages/api/auth/route.ts

import { NextApiRequest, NextApiResponse } from "next"
import { toNodeHandler } from "better-auth/node"
import { auth } from "@/lib/auth"

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    await toNodeHandler(auth)(req, res)
}

however, this has not fixed the issue. would appreciate any pointers on how to fix this, thanks!

<!-- gh-comment-id:2557888010 --> @drshika commented on GitHub (Dec 20, 2024): Hello! I think I might be missing something here. I ran into this issue: Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported. I tried adding in the Node Adapter as mentioned earlier to src/pages/api/auth/route.ts ```js import { NextApiRequest, NextApiResponse } from "next" import { toNodeHandler } from "better-auth/node" import { auth } from "@/lib/auth" export default async function handler(req: NextApiRequest, res: NextApiResponse) { await toNodeHandler(auth)(req, res) } ``` however, this has not fixed the issue. would appreciate any pointers on how to fix this, thanks!
Author
Owner

@PhillJK commented on GitHub (Dec 23, 2024):

Hello! I think I might be missing something here. I ran into this issue:

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported.

I tried adding in the Node Adapter as mentioned earlier to src/pages/api/auth/route.ts

import { NextApiRequest, NextApiResponse } from "next"
import { toNodeHandler } from "better-auth/node"
import { auth } from "@/lib/auth"

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    await toNodeHandler(auth)(req, res)
}

however, this has not fixed the issue. would appreciate any pointers on how to fix this, thanks!

Hi, I have not met this issue. Maybe something is wrong with your import/export settings.

Basically I have tried this

file path: /api/auth/[...all].js

import { toNodeHandler } from "better-auth/node";
import { auth } from "../../../utils/better-auth";

export const config = {
    api: {
        bodyParser: false,
    },
};

const authHandler = toNodeHandler(auth);

export default authHandler;

And it worked fine

<!-- gh-comment-id:2559922922 --> @PhillJK commented on GitHub (Dec 23, 2024): > Hello! I think I might be missing something here. I ran into this issue: > > Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/nanostores/index.js from .../node_modules/better-auth/dist/react.cjs not supported. > > I tried adding in the Node Adapter as mentioned earlier to src/pages/api/auth/route.ts > > ```js > import { NextApiRequest, NextApiResponse } from "next" > import { toNodeHandler } from "better-auth/node" > import { auth } from "@/lib/auth" > > export default async function handler(req: NextApiRequest, res: NextApiResponse) { > await toNodeHandler(auth)(req, res) > } > ``` > > however, this has not fixed the issue. would appreciate any pointers on how to fix this, thanks! Hi, I have not met this issue. Maybe something is wrong with your import/export settings. Basically I have tried this file path: /api/auth/[...all].js ```js import { toNodeHandler } from "better-auth/node"; import { auth } from "../../../utils/better-auth"; export const config = { api: { bodyParser: false, }, }; const authHandler = toNodeHandler(auth); export default authHandler; ``` And it worked fine
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8466