fix: make mongodb import lazy in casbinMongoRule (type-only import)#82
Open
JohannesWill wants to merge 1 commit into
Open
fix: make mongodb import lazy in casbinMongoRule (type-only import)#82JohannesWill wants to merge 1 commit into
JohannesWill wants to merge 1 commit into
Conversation
Use a type-only import for `ObjectId` in casbinMongoRule so importing
the package no longer emits a top-level `require('mongodb')`. mongodb is
only an optional peer dependency of typeorm and is used solely as a type
here, so non-MongoDB consumers were crashing at import time with
MODULE_NOT_FOUND. With `import type`, no runtime reference to mongodb is
emitted. MongoDB users are unaffected since typeorm loads the driver
lazily when a Mongo DataSource is initialized.
focode
approved these changes
Jul 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since
1.10.0, importingtypeorm-adapterthrowsCannot find module 'mongodb'(MODULE_NOT_FOUND) for any consumer that does not havemongodbinstalled — even when using a different driver (PostgreSQL, SQLite, …).src/index.tsre-exportscasbinMongoRule, andsrc/casbinMongoRule.tsimportsmongodbat the top level.mongodbis only an optional peer dependency oftypeormand merely a devDependency here, so it is absent in most production installs.Fix
ObjectIdis only used as a type (public id!: ObjectId;). Switching to a type-only import removes the runtimerequire('mongodb'): withemitDecoratorMetadata, TypeScript no longer emits a reference tomongodb.ObjectId.Verification
Compiled output no longer contains any reference to
mongodb:grep -c mongodb lib/casbinMongoRule.js→0. Build (tsc) and lint pass.MongoDB users are unaffected — TypeORM loads the mongodb driver lazily via its own
PlatformToolswhen a MongoDataSourceis actually initialized.Closes #81