Error connecting to mongoose when using deno cli
deno-x86_64-pc-windows-msvc v1.41.1
Testing the following code using
nodejs
and on deno deploy
works correctly:
But when testing using deno cli
it doesn't work.
I get the message on the console starting...
but not the connection error:
neither Connected to MongoDB
And after some time I get this error:
The same thing happens using mongodb
instead of mongoose
:
I'm starting the script as
I also tested running it as admin
, on the Mongo website I have all ip whitelisted
Why do I get this error only on the CLI version?56 Replies
When working with MongoDB in Deno, the documentation suggests using the official MongoDB driver with npm specifiers. This means that instead of using Mongoose, which is a higher-level abstraction built for Node.js, you should use the MongoDB driver directly. Here's an example from the documentation on how to connect to MongoDB using the MongoDB driver in Deno:
To run this example, you would use the Deno CLI with the appropriate permissions:
The error you're encountering with Mongoose may be due to compatibility issues since Mongoose is designed to work with Node.js and not directly with Deno. Deno has a different runtime environment and module resolution system, which could lead to unexpected behavior when trying to use Node.js-specific packages like Mongoose.
To resolve your issue, consider using the MongoDB driver directly as shown in the documentation example. Make sure to replace the connection string with your actual MongoDB URI and adjust the database and collection names as needed.
If you need to use Mongoose for some reason, you might want to check if there's an updated version or a Deno-specific fork that is compatible with Deno's runtime. However, based on the documentation, the recommended approach is to use the MongoDB driver with npm specifiers. 🦕
is someone else able to reproduce this issue?
https://docs.deno.com/runtime/tutorials/how_to_with_npm/mongoose
Did you follow this tutorial? Seems like there is something with your import maybe?
How to use Mongoose with Deno | Deno Docs
Mongoose is a popular, schema-based library that
its fails even with
Mongodb
and the same code works on deno deploy/ local nodejsdo you use 'npm:mongoose...' in your deno file?
no
i have the npm imports in deno.json
oh ok, i never used like this, maybe try with this :
import { model, Schema } from "npm:mongoose@^6.7";
import mongoose from "npm:mongoose@^6.7";
it doesnt work either, getting similar error, does it work at your side?
and it works. i did in the past get the same error, probably user/password incorrectlly, i don't know but that's how i did
i tried your code it also throws a connection timed out error
how you are starting your script, is it ts or js?
i even tried creating a new mongo db
same error
deno run -A --watch main.ts
Deno need permission, this command should work, i hope
could you try with a js project?
oh ok, sorry i didn't read fully the error prompt
i tried the same project with node and deno
do i need something else? looks like its stopped after that watcher message
ah it started now
same thing :/
on node works perfect
hm strange indeed i don't know what to say beside, work on node ? x)
`
Try to use this parameter with a higher value
does your test project contain any other file other than the index.js?
no i just tried to connect to Mongo Atlas
i mean if the cli require you to configure something different
like the npm: thing
I use VS Code with the Deno extension. I just had to cache the npm:mongoose thingy and that's all
i hate vscode, ill try with it
what do you use then?
visual studio
oh i see, well, i don't know how it works on Visual Studio but i highly recommend to use VS Code. Or you willl have to use the CLI often
same error
try to add the serverSelectionTimeoutMS with 10 second, maybe it need more time?
, {
serverSelectionTimeoutMS: 10000
}
i tried now, same thing :/
hm well i don't know, but at least it works on node which is better than nothing x)
yes on node works correctly
deno is pissing me off
i don't really know why it doesn't work but the documentation says it's better to use the mongodb driver and not mongoose
it also doesnt work with mongodb
i will try with mongodb on deno
could you try with js instead of ts
sure
`
const client = new MongoClient('***',{
connectTimeoutMS:5000,
});
we never know
worked?
yup for me
whats your deno cli version
and your os
deno : 1.41.1
windows 10
same here
you could always install MongoCompass on your computer and connect to it
i already have it
oh ok
im connecting to it, not locally
you use MongoCompass to connect to MongoAtlas?
no, i mean that i'm not connecting to a local db
i update to deno 1.41.2 (with deno upgrade) and change
npm:mongodb@6.4.0
It works too
im getting
ISOLATE_INTERNAL_FAILURE
on deno deplo , i was trying to use deno cli to debug it:
https://discord.com/channels/684898665143206084/826085979344470037/1215689372305723444
I'm thinking of switching to Heroku, already wasted a lot of time withyour entrypoint is your server file?
if you are ready to pay x) i think there is another option for free hosting
wdym?
paying is not a problem
deployctl deploy --project=helloworld --entrypoint=src/entrypoint.ts => should point to your server file
im using the website not the deployctl
my entrypoint is the index.js
ok, i never used a git repo to deploy on deno deploy, my bad
try using deno deploy ctl, maybe it'll work? who knows
i dont know how to use it, ill read about
https://docs.deno.com/deploy/manual/deployctl there you go if you need informations
Using deployctl on the command line | Deno Docs
deployctl is a command line tool (CLI) that lets you work with the Deno Deploy