[GH-ISSUE #1492] mongodb dependency requirement for mongodbAdapter() #8786

Closed
opened 2026-04-13 03:59:25 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @firatciftci on GitHub (Feb 19, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1492

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I have a Next.js App Router application that utilizes both mongoose and better-auth, where better-auth uses the mmongodbAdapter() to hook into my MongoDB database, as such:

import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import mongoose from "mongoose";
import dbConnect from "./db-connect";

await dbConnect();
const client = mongoose.connection.db;

export const auth = betterAuth({
  database: mongodbAdapter(client),
  ...
});

Here, the typings for client acquired via mongoose match what mongodbAdapter() expects without any issues. However, when I try to run the development server, I get the following error:

 ⨯ ./node_modules/.pnpm/better-auth@1.1.18/node_modules/better-auth/dist/adapters/mongodb.js:12:1
Module not found: Can't resolve 'mongodb'
  10 | import '../chunk-TQQSPPNA.js';
  11 | import '../chunk-UNWCXKMP.js';
> 12 | import { ObjectId } from 'mongodb';
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13 |
  14 | var createTransform = (options) => {
  15 |   const schema = getAuthTables(options);

As I use mongoose in my project, I do not have the mongodb dependency installed, and I would really prefer keeping my list of dependencies clean. Seeing that mongodbAdapter() only uses ObjectId from mongodb, is there any way to target Types.ObjectId from mongoose if the project has mongoose installed? Alternatively, would it be possible to use ObjectId from bson or any non-mongodb dependency to resolve this problem?

Current vs. Expected behavior

I expect mongodbAdapter() to work with mongoose solely, without any issues. Currently, I get an error due to missing mongodb dependency.

What version of Better Auth are you using?

1.1.18

Provide environment information

- OS: macOS 15.3
- Browser: Chrome 133.0.6943.55

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

Backend

Auth config (if applicable)

import { hash, verify } from "@node-rs/argon2";
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { nextCookies } from "better-auth/next-js";
import { username } from "better-auth/plugins";
import mongoose from "mongoose";
import dbConnect from "./db-connect";

await dbConnect();
const client = mongoose.connection.db;

export const auth = betterAuth({
  database: mongodbAdapter(client),
  plugins: [username(), nextCookies()],
  rateLimit: {
    enabled: true,
  },
  emailAndPassword: {
    enabled: true,
    password: {
      async hash(password) {
        return hash(password, {
          memoryCost: 19_456,
          timeCost: 2,
          outputLen: 32,
          parallelism: 1,
        });
      },
      async verify({ hash, password }) {
        return verify(hash, password, {
          memoryCost: 19_456,
          timeCost: 2,
          outputLen: 32,
          parallelism: 1,
        });
      },
    },
  },
  user: {
    modelName: "usersNew",
  },
  session: {
    modelName: "sessions",
  },
  account: {
    modelName: "accounts",
  },
  verification: {
    modelName: "verifications",
  },
});

Additional context

No response

Originally created by @firatciftci on GitHub (Feb 19, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1492 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I have a Next.js App Router application that utilizes both `mongoose` and `better-auth`, where `better-auth` uses the `mmongodbAdapter()` to hook into my MongoDB database, as such: ```ts import { betterAuth } from "better-auth"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import mongoose from "mongoose"; import dbConnect from "./db-connect"; await dbConnect(); const client = mongoose.connection.db; export const auth = betterAuth({ database: mongodbAdapter(client), ... }); ``` Here, the typings for `client` acquired via `mongoose` match what `mongodbAdapter()` expects without any issues. However, when I try to run the development server, I get the following error: ``` ⨯ ./node_modules/.pnpm/better-auth@1.1.18/node_modules/better-auth/dist/adapters/mongodb.js:12:1 Module not found: Can't resolve 'mongodb' 10 | import '../chunk-TQQSPPNA.js'; 11 | import '../chunk-UNWCXKMP.js'; > 12 | import { ObjectId } from 'mongodb'; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 13 | 14 | var createTransform = (options) => { 15 | const schema = getAuthTables(options); ``` As I use `mongoose` in my project, I do not have the `mongodb` dependency installed, and I would really prefer keeping my list of dependencies clean. Seeing that `mongodbAdapter()` only uses `ObjectId` from `mongodb`, is there any way to target `Types.ObjectId` from `mongoose` if the project has `mongoose` installed? Alternatively, would it be possible to use `ObjectId` from `bson` or any non-`mongodb` dependency to resolve this problem? ### Current vs. Expected behavior I expect `mongodbAdapter()` to work with `mongoose` solely, without any issues. Currently, I get an error due to missing `mongodb` dependency. ### What version of Better Auth are you using? 1.1.18 ### Provide environment information ```bash - OS: macOS 15.3 - Browser: Chrome 133.0.6943.55 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { hash, verify } from "@node-rs/argon2"; import { betterAuth } from "better-auth"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { nextCookies } from "better-auth/next-js"; import { username } from "better-auth/plugins"; import mongoose from "mongoose"; import dbConnect from "./db-connect"; await dbConnect(); const client = mongoose.connection.db; export const auth = betterAuth({ database: mongodbAdapter(client), plugins: [username(), nextCookies()], rateLimit: { enabled: true, }, emailAndPassword: { enabled: true, password: { async hash(password) { return hash(password, { memoryCost: 19_456, timeCost: 2, outputLen: 32, parallelism: 1, }); }, async verify({ hash, password }) { return verify(hash, password, { memoryCost: 19_456, timeCost: 2, outputLen: 32, parallelism: 1, }); }, }, }, user: { modelName: "usersNew", }, session: { modelName: "sessions", }, account: { modelName: "accounts", }, verification: { modelName: "verifications", }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 03:59:25 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jun 12, 2025):

Hi, @firatciftci. I'm Dosu, and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • The issue involves a Next.js application using better-auth with the mongodbAdapter().
  • The current setup requires the mongodb dependency for ObjectId, causing errors when using mongoose without mongodb.
  • You suggested alternatives like using Types.ObjectId from mongoose or ObjectId from bson.
  • The goal is to streamline the dependency list by avoiding the mongodb dependency.
  • The issue is observed with Better Auth version 1.1.18 on macOS 15.3.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of the better-auth repository. If so, you can keep the discussion open by commenting here.
  • Otherwise, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

<!-- gh-comment-id:2967419159 --> @dosubot[bot] commented on GitHub (Jun 12, 2025): Hi, @firatciftci. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - The issue involves a Next.js application using `better-auth` with the `mongodbAdapter()`. - The current setup requires the `mongodb` dependency for `ObjectId`, causing errors when using `mongoose` without `mongodb`. - You suggested alternatives like using `Types.ObjectId` from `mongoose` or `ObjectId` from `bson`. - The goal is to streamline the dependency list by avoiding the `mongodb` dependency. - The issue is observed with Better Auth version 1.1.18 on macOS 15.3. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of the better-auth repository. If so, you can keep the discussion open by commenting here. - Otherwise, this issue will be automatically closed in 7 days. Thank you for your understanding and contribution!
Author
Owner

@firatciftci commented on GitHub (Jun 18, 2025):

This issue is still present; I have to have mongodb installed as a dependency even though I am not using it and mongoose is the primary way I interface with my MongoDB database. I would really prefer not having to install mongodb alongside mongoose, if possible.

I have the MongoDB adapter set up as such:

import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
import { connection, Types } from "mongoose";
import dbConnect from "./db-connect";

await dbConnect();
const client = connection.db!;

export const auth = betterAuth({
  database: mongodbAdapter(client),
  ...

Here is the error I get when I start the development server (without mongodb installed):

./node_modules/.pnpm/better-auth@1.2.9/node_modules/better-auth/dist/adapters/mongodb-adapter/index.mjs:1:1
Module not found: Can't resolve 'mongodb'
> 1 | import { ObjectId } from 'mongodb';
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2 | import '../../shared/better-auth.Cc72UxUH.mjs';
  3 | import '../../shared/better-auth.8zoxzg-F.mjs';
  4 | import '@better-auth/utils/random';

Based on this error, if the main requirement for the mongodb package is the ObjectId import, perhaps the standalone bson-objectid package is more suited for the task?

<!-- gh-comment-id:2985699507 --> @firatciftci commented on GitHub (Jun 18, 2025): This issue is still present; I have to have `mongodb` installed as a dependency even though I am not using it and `mongoose` is the primary way I interface with my MongoDB database. I would really prefer not having to install `mongodb` alongside `mongoose`, if possible. I have the MongoDB adapter set up as such: ```ts import { betterAuth } from "better-auth"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; import { connection, Types } from "mongoose"; import dbConnect from "./db-connect"; await dbConnect(); const client = connection.db!; export const auth = betterAuth({ database: mongodbAdapter(client), ... ``` Here is the error I get when I start the development server (without `mongodb` installed): ``` ./node_modules/.pnpm/better-auth@1.2.9/node_modules/better-auth/dist/adapters/mongodb-adapter/index.mjs:1:1 Module not found: Can't resolve 'mongodb' > 1 | import { ObjectId } from 'mongodb'; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 | import '../../shared/better-auth.Cc72UxUH.mjs'; 3 | import '../../shared/better-auth.8zoxzg-F.mjs'; 4 | import '@better-auth/utils/random'; ``` Based on this error, if the main requirement for the `mongodb` package is the `ObjectId` import, perhaps the standalone [bson-objectid](https://www.npmjs.com/package/bson-objectid) package is more suited for the task?
Author
Owner

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

Hi, @firatciftci. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported a module resolution error when using mongodbAdapter() in a Next.js app that uses mongoose but does not have the mongodb package installed.
  • The error occurs because mongodbAdapter imports ObjectId from the mongodb package, which your app does not include.
  • You requested if the adapter could use mongoose's ObjectId or a lighter dependency like bson-objectid to avoid requiring the full mongodb package.
  • You confirmed the issue still occurs with better-auth version 1.2.9 and provided detailed error information.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open.
  • If I do not hear back within 7 days, this issue will be automatically closed.

Thank you for your understanding and contribution!

<!-- gh-comment-id:3303664210 --> @dosubot[bot] commented on GitHub (Sep 17, 2025): Hi, @firatciftci. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported a module resolution error when using `mongodbAdapter()` in a Next.js app that uses mongoose but does not have the `mongodb` package installed. - The error occurs because `mongodbAdapter` imports `ObjectId` from the `mongodb` package, which your app does not include. - You requested if the adapter could use mongoose's `ObjectId` or a lighter dependency like `bson-objectid` to avoid requiring the full `mongodb` package. - You confirmed the issue still occurs with better-auth version 1.2.9 and provided detailed error information. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here to keep the discussion open. - If I do not hear back within 7 days, this issue will be automatically closed. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8786