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
- Fresh project, install only
typeorm-adapter@1.10.0 and a non-mongo driver (e.g. pg or better-sqlite3). Do not install mongodb.
- 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.
Description
Since
typeorm-adapter@1.10.0, importing the package throwsCannot find module 'mongodb'at load time for any consumer that does not havemongodbinstalled — even when they use a completely different database driver (e.g. PostgreSQL/SQLite).mongodbis only an optional peer dependency oftypeorm(seetypeorm@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:
and that module imports
mongodbeagerly at the top level:So
require('typeorm-adapter')always evaluatescasbinMongoRule, which always requiresmongodb. For non-Mongo users this fails immediately.Steps to reproduce
typeorm-adapter@1.10.0and a non-mongo driver (e.g.pgorbetter-sqlite3). Do not installmongodb.node -e "require('typeorm-adapter')"Actual behavior
Expected behavior
Importing
typeorm-adaptershould not requiremongodbunless the MongoDB code path is actually used, consistent with how TypeORM treatsmongodbas an optional/lazy driver.Environment
typeorm-adapter: 1.10.0typeorm: 1.0.01.9.0, where importing the package worked withoutmongodbpresent.Suggested fix
Make the MongoDB dependency lazy so it is only required when the Mongo adapter path runs. Options:
casbinMongoRulefrom the top-levelindex.ts; expose it via a separate subpath entry (e.g.typeorm-adapter/mongo), orimport { ObjectId } from 'mongodb'with a lazyrequire('mongodb')inside the code path that actually builds/usesCasbinMongoRule, guarded so absence ofmongodbdoesn't crash non-Mongo consumers.