Open kaushiksahu18 opened 1 month ago
I'm unable to repro, the following script works fine with Node.js. Please modify the following script to demonstrate your issue. Also, can you please clarify whether you're using Next.js or some environment other than Node.js, and also which version of TypeScript you're using and your TypeScript config? I see you have an src/utils.ts
file.
import { serve } from '@hono/node-server';
import { Hono } from 'hono';
import mongoose from 'mongoose';
const app = new Hono()
app.get('/', async (c) => {
await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
return c.text('Hono meets Node.js')
})
serve(app, (info) => {
console.log(`Listening on http://localhost:${info.port}`) // Listening on http://localhost:3000
})
Output:
$ node gh-14613.mjs
Listening on http://localhost:3000
Curl command:
$ curl http://localhost:3000
Hono meets Node.js
$
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days
the following script works fine with Node.js.
I have used for cloudflare workers (wrangler)
TS version - latest
The easiest way to fix this particular error is to add the following to your wrangler.toml
file:
node_compat = true
However, with that, we still get the following errors:
▲ [WARNING] The package "node:async_hooks" wasn't found on the file system but is built into node.
Your Worker may throw errors at runtime unless you enable the "nodejs_compat" compatibility flag.
Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details. Imported
from:
- node_modules/mongoose/lib/mongoose.js
✘ [ERROR] Could not resolve "fs/promises"
node_modules/mongodb/lib/client-side-encryption/state_machine.js:4:19:
4 │ const fs = require("fs/promises");
╵ ~~~~~~~~~~~~~
The package "fs/promises" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
I'm investigating a fix. But it's worth mentioning that we haven't tested Mongoose with Cloudflare Workers at all.
I took some time to play whack-a-mole with all the build issues that pop up with Cloudflare Workers, but still no luck, Mongoose can't connect. It looks like Cloudflare Workers has some support for TCP sockets, but looks like that doesn't quite work with the MongoDB driver.
It looks like the MongoDB driver doesn't support Cloudflare Workers, the officially supported way to connect to MongoDB from Cloudflare Workers is Realm. Mongoose doesn't have a Realm driver, so we can't quite use that yet. But we may consider adding a Realm driver in the future.
Prerequisites
Mongoose version
8.4.0
Node.js version
20.11.1
MongoDB server version
7.0.8
Typescript version (if applicable)
No response
Description
i was trying to use mongodb with honojs and i have installed mongoos but
connectDB
function is showing the errorimport_mongoose.default.connect is not a function
log
Steps to Reproduce