Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type 'import(".../node_modules/mongoose/node_modules/mongodb/mongodb").MongoClient' is not assignable to type 'import(".../node_modules/mongodb/mongodb").MongoClient'. #14653

Closed
2 tasks done
Saffr3n opened this issue Jun 7, 2024 · 7 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@Saffr3n
Copy link

Saffr3n commented Jun 7, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.1

Node.js version

20.14.0

MongoDB server version

6.7.0

Typescript version (if applicable)

5.4.5

Description

When I create MongoStore using connect-mongo npm package, and provide MongoClient with mongoose.client.getClient(), vscode shows me error from the issue's title.
I'm not sure if this is a mongoose bug or not, but it started happening after I updated mongoose to the last version.

connect-mongo version: 5.1.0

npm explain mongodb
mongodb@6.7.0 peer
node_modules/mongodb
  peer mongodb@">= 5.1.0 < 7" from connect-mongo@5.1.0
  node_modules/connect-mongo
    connect-mongo@"^5.1.0" from the root project

mongodb@6.6.2
node_modules/mongoose/node_modules/mongodb
  mongodb@"6.6.2" from mongoose@8.4.1
  node_modules/mongoose
    mongoose@"^8.4.1" from the root project

Steps to Reproduce

import MongoStore from 'connect-mongo';
import mongoose from 'mongoose';

MongoStore.create({ client: mongoose.connection.getClient() });

This will show an error in vscode:

Type 'import(".../node_modules/mongoose/node_modules/mongodb/mongodb").MongoClient' is not assignable to type 'import(".../node_modules/mongodb/mongodb").MongoClient'.
  The types returned by 'db(...).createCollection(...)' are incompatible between these types.
    Type 'Promise<import(".../node_modules/mongoose/node_modules/mongodb/mongodb").Collection<TSchema>>' is not assignable to type 'Promise<import(".../node_modules/mongodb/mongodb").Collection<TSchema>>'.
      Type 'import(".../node_modules/mongoose/node_modules/mongodb/mongodb").Collection<TSchema>' is not assignable to type 'import(".../node_modules/mongodb/mongodb").Collection<TSchema>'.
        The types returned by 'bulkWrite(...)' are incompatible between these types.
          Type 'Promise<import(".../node_modules/mongoose/node_modules/mongodb/mongodb").BulkWriteResult>' is not assignable to type 'Promise<import(".../node_modules/mongodb/mongodb").BulkWriteResult>'.
            Type 'import(".../node_modules/mongoose/node_modules/mongodb/mongodb").BulkWriteResult' is not assignable to type 'import(".../node_modules/mongodb/mongodb").BulkWriteResult'.
              Types have separate declarations of a private property 'result'.ts(2322)
MongoStore.d.ts(15, 5): The expected type comes from property 'client' which is declared here on type 'ConnectMongoOptions'

Expected Behavior

No response

@vkarpov15
Copy link
Collaborator

This looks to be expected behavior because for some reason connect-mongo is installing mongodb@6.7.0 but Mongoose has mongodb@6.6.2. Are you using npm, yarn, something else?

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jun 7, 2024
@Saffr3n
Copy link
Author

Saffr3n commented Jun 8, 2024

Are you using npm, yarn, something else?

npm

@Saffr3n
Copy link
Author

Saffr3n commented Jun 10, 2024

I don't know why typescript doesn't like it. I've read declaration files for both (node_modules/mongodb/mongodb).MongoClient and (node_modules/mongoose/node_modules/mongodb/mongodb).MongoClient. They look identical.

I did this for now to get rid of that:

declare module 'mongoose' {
  interface Connection {
    getClient(): import('mongodb').MongoClient;
  }
}

@vkarpov15
Copy link
Collaborator

Do you have mongodb in your package.json dependencies or devDependencies?

@Saffr3n
Copy link
Author

Saffr3n commented Jun 10, 2024

Do you have mongodb in your package.json dependencies or devDependencies?

I tried doing npm i mongodb, it just added mongodb to my dependencies, but of course it didn't download anything, because I already have the latest version in my node_modules thanks to connect-mongo. Either way, with or without mongodb in my package.json, the issue remains.

@vkarpov15
Copy link
Collaborator

Try running npm i mongodb@6.6.2

@Saffr3n
Copy link
Author

Saffr3n commented Jun 22, 2024

Try running npm i mongodb@6.6.2

Idk why I didn't come up with this one before. Worked like a charm. Thank you!

@Saffr3n Saffr3n closed this as completed Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants