unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.96k stars 496 forks source link

mongodb_connection_string_url_1.default is not a constructor #1024

Closed lautiamkok closed 4 months ago

lautiamkok commented 1 year ago

Environment

Node.js: v19 nitropack: v2.2.3 mongodb: v5.1.0

Reproduction

// routes/index.ts

import { MongoClient } from 'mongodb'

export default eventHandler(async event => {
  const mongoClient = await new MongoClient('mongodb://localhost:27017').connect()
  const collection = mongoClient.db('testdb').collection('testcollection')
  const docs = await collection.find().toArray()
  client.close()
  return docs
})

Describe the bug

Get the following error on my terminal:

[nitro] [request error] [unhandled] mongodb_connection_string_url_1.default is not a constructor
  at parseOptions (/.../.nitro/dev/index.mjs:13395:19)  
  at new MongoClient (/.../.nitro/dev/index.mjs:19349:64)  
  at /.../.nitro/dev/index.mjs:20962:29  
  at /.../.nitro/dev/index.mjs:927:47  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at async Object.handler (/.../.nitro/dev/index.mjs:983:19)  
  at async Server.toNodeHandle (/.../.nitro/dev/index.mjs:1037:7)
handling cors
✔ Nitro built in 2194 ms 

Additional context

No issue with the exact same code on the app that uses h3 directly though.

Logs

No response

manniL commented 1 year ago

Might be related: https://github.com/unjs/nitro/issues/949

pi0 commented 1 year ago

Can you please share a reproduction? 🙏🏼 (also what is your deployment target)

lautiamkok commented 1 year ago

@pi0

Here you go:

https://github.com/lautiamkok/Nitro-MongoDB

Would like to deploy it to a Node.js server as an API on a custom port like 5000.

Dav3rs commented 1 year ago

I'm having the same issue, and I just moved the same exact code to a nuxt api route and it woks! Currently using nuxt as a backend microservice because of this 😅, without pages, just consuming the api routes.

Hebilicious commented 1 year ago

@lautiamkok I can't access your reproduction. Please provide a new one either with github or stackblitz. This might a duplicate as @manniL suggested.

CooLNuanfeng commented 1 year ago

+1

pi0 commented 4 months ago

Closing due to lack of reproduction. It is likely that you face this issue when trying to bundle mongo for edge-targets. Please consider that mongodb package is not compatible with edge.