JohannesWill opened a new issue, #81: URL: https://github.com/apache/casbin-typeorm-adapter/issues/81
## Description Since `[email protected]`, 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 `[email protected]`, `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: ```ts // src/index.ts export * from './casbinMongoRule'; ``` and that module imports `mongodb` eagerly at the top level: ```ts // 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 `[email protected]` 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
