Skip to content

v1.10.0: hard import 'mongodb' in casbinMongoRule breaks all non-MongoDB users (MODULE_NOT_FOUND) #81

Description

@JohannesWill

Description

Since typeorm-adapter@1.10.0, importing the package throws Cannot find module 'mongodb' at load time for any consumer that does not have mongodb installed — even when they use a completely different database driver (e.g. PostgreSQL/SQLite).

mongodb is only an optional peer dependency of typeorm (see typeorm@1.0.0, peerDependenciesMeta.mongodb.optional === true) and merely a devDependency of this adapter, so it is not installed in production for the vast majority of users.

Root cause

The main barrel unconditionally re-exports the Mongo rule entity:

// src/index.ts
export * from './casbinMongoRule';

and that module imports mongodb eagerly at the top level:

// src/casbinMongoRule.ts
import { ObjectId } from 'mongodb';

So require('typeorm-adapter') always evaluates casbinMongoRule, which always requires mongodb. For non-Mongo users this fails immediately.

Steps to reproduce

  1. Fresh project, install only typeorm-adapter@1.10.0 and a non-mongo driver (e.g. pg or better-sqlite3). Do not install mongodb.
  2. Run: node -e "require('typeorm-adapter')"

Actual behavior

Error: Cannot find module 'mongodb'
Require stack:
- .../node_modules/typeorm-adapter/lib/casbinMongoRule.js
- .../node_modules/typeorm-adapter/lib/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1476:15)
    ...
    at Object.<anonymous> (.../typeorm-adapter/lib/casbinMongoRule.js:26:19)
  code: 'MODULE_NOT_FOUND'

Expected behavior

Importing typeorm-adapter should not require mongodb unless the MongoDB code path is actually used, consistent with how TypeORM treats mongodb as an optional/lazy driver.

Environment

  • typeorm-adapter: 1.10.0
  • typeorm: 1.0.0
  • Node.js: 24.x
  • Regression vs. 1.9.0, where importing the package worked without mongodb present.

Suggested fix

Make the MongoDB dependency lazy so it is only required when the Mongo adapter path runs. Options:

  • Don't re-export casbinMongoRule from the top-level index.ts; expose it via a separate subpath entry (e.g. typeorm-adapter/mongo), or
  • Replace the top-level import { ObjectId } from 'mongodb' with a lazy require('mongodb') inside the code path that actually builds/uses CasbinMongoRule, guarded so absence of mongodb doesn't crash non-Mongo consumers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions