Error Importing "mongodb" with typescript
Asked Answered
M

5

11

When compiling any typescript program that just imports mongodb, i get 12 errors like:

node_modules/mongodb/mongodb.d.ts:3309:5 - error TS2416: Property 'end' in type 'GridFSBucketWriteStream' is not assignable to the same property in base type 'WritableStream'

To reproduce is simple with node TLS (v16.13.1), & a empty directory, just run:

npm i mongodb typescript
echo "import mongodb  from 'mongodb'" > index.ts
npx tsc index
Mailable answered 6/1, 2022 at 12:38 Comment(5)
I think your example has a mistake. It should be import * as mongodb from 'mongodb' since mongodb does not have a default export.Commerce
I'm encountering the same issue, though using Mongoose, which depends on MongoDB's native Node driver. The solution I came to was downgrading Mongoose to the last major release, which I assume downgraded the MongoDB driver. Not sure if that fits your usecase.Embracery
I too just encountered this same error today. mongodb 4.2.2 Not using mongoose.Mobilize
Thanks for the suggestion Sam, I changed the example to your suggestion, but got the same resultMailable
Thanks Quontas, I can also get it to work with the v3.x.x of the mongdb driver, but none of the 4.x.x versions work!Mailable
M
4

Looks like mongodb npm released 4.3.0 last night (2022-01-06) that now resolves this issue! changelog includes the issue here: #3088

v4 now compiles without these errors!

Mailable answered 7/1, 2022 at 12:9 Comment(0)
L
7

i referred this https://github.com/mongodb/node-mongodb-native/pull/3088

npm i @types/[email protected] --save-dev
Louisville answered 15/1, 2022 at 7:21 Comment(1)
That one worked for me.Tamekia
M
4

Looks like mongodb npm released 4.3.0 last night (2022-01-06) that now resolves this issue! changelog includes the issue here: #3088

v4 now compiles without these errors!

Mailable answered 7/1, 2022 at 12:9 Comment(0)
P
2

I have to downgrade my @types/node to version 17.0.0

"@types/node": "17.0.0", worked for me

read more: https://github.com/mongodb/node-mongodb-native/pull/3088

Pheni answered 25/1, 2022 at 18:41 Comment(0)
H
1

mongodb 4.3.0 fix this issue for me. I use mongoose but I put "mongodb": "^4.3.0" in my dependencies to force the update. I don't know if this is the right thing to do but it works while waiting for mongoose to update.

Haslett answered 7/1, 2022 at 22:58 Comment(0)
C
0

I'm unable to reproduce your problem in my local environment, so I think it is a problem probably related to some other package in your project.

As suggested on this issue, you should try deleting your package-lock.json and generating it again with npm.

Commerce answered 6/1, 2022 at 15:48 Comment(2)
I tried this as suggested, and additionally, removed my "node-modules" folder. This did not solve the problem for me.Mobilize
Thanks for your suggestion, I've just tried it on a 2nd machine, Ubuntu 21.04 again, with the same result. There is no package-lock.json before I run the "npm i mongodb typescript" it just gets the latest versions. I also tried to delete the $HOME/.npm folder with no luck. I dont understand itMailable

© 2022 - 2024 — McMap. All rights reserved.