tegaki
tegaki11mo ago

unable to connect to mongodb using deno inside container

First of all, I want to mention that when I run deno task preview on my local machine / laptop, it works 🎉 . No issues there. But I need to run my deno program inside a container for depolyment reasons (currently using fly.io) it fails. I'm using the official mongodb npm client. I use it via an import map (maybe that matters?), deno.json:
{
"imports": {
"db": "npm:mongodb@6.3.0",
}
}
{
"imports": {
"db": "npm:mongodb@6.3.0",
}
}
import { MongoClient } from "db";
// ...
const mongoClient = new MongoClient(url);
await mongoClient.connect();
// ...
import { MongoClient } from "db";
// ...
const mongoClient = new MongoClient(url);
await mongoClient.connect();
// ...
And when I try to run the code in a container I get the following error:
Task preview deno run -A main.ts
error: Uncaught (in promise) Error: Cannot find module 'bson'
Require stack:
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/admin.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/index.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/index.js
at Function.Module._resolveFilename (node:module:622:15)
at Function.Module._load (node:module:500:27)
at Module.require (node:module:675:19)
at require (node:module:789:16)
at Object.<anonymous> (file:///app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js:4:14)
at Object.<anonymous> (file:///app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js:63:4)
at Module._compile (node:module:731:34)
at Object.Module._extensions..js (node:module:745:10)
at Module.load (node:module:656:32)
at Function.Module._load (node:module:537:12)
Task preview deno run -A main.ts
error: Uncaught (in promise) Error: Cannot find module 'bson'
Require stack:
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/admin.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/index.js
- /app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/index.js
at Function.Module._resolveFilename (node:module:622:15)
at Function.Module._load (node:module:500:27)
at Module.require (node:module:675:19)
at require (node:module:789:16)
at Object.<anonymous> (file:///app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js:4:14)
at Object.<anonymous> (file:///app/node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js:63:4)
at Module._compile (node:module:731:34)
at Object.Module._extensions..js (node:module:745:10)
at Module.load (node:module:656:32)
at Function.Module._load (node:module:537:12)
checklist: - same version of deno both places - I've tried connecting to mongodb with the deno package mongo but I get another error about the driver not supporting load balancing... so I guess it's just out of date? (6 months since last release) - I've tried to base my image on ubuntu:latest (install deno + run) same issue 🤔
5 Replies
tegaki
tegakiOP11mo ago
root@dee72538d329:/app# find -name 'bson*'
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/src/bson.ts
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js.map
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js
./node_modules/.deno/mongodb@6.3.0/node_modules/bson
./node_modules/.deno/node_modules/bson
./node_modules/.deno/bson@6.3.0
./node_modules/.deno/bson@6.3.0/node_modules/bson
./node_modules/.deno/bson@6.3.0/node_modules/bson/src/bson.ts
./node_modules/.deno/bson@6.3.0/node_modules/bson/src/bson_value.ts
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.bundle.js.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.cjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.cjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.mjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.mjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.bundle.js
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.rn.cjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.rn.cjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/bson.d.ts
root@dee72538d329:/app# find -name 'bson*'
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/src/bson.ts
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js.map
./node_modules/.deno/mongodb@6.3.0/node_modules/mongodb/lib/bson.js
./node_modules/.deno/mongodb@6.3.0/node_modules/bson
./node_modules/.deno/node_modules/bson
./node_modules/.deno/bson@6.3.0
./node_modules/.deno/bson@6.3.0/node_modules/bson
./node_modules/.deno/bson@6.3.0/node_modules/bson/src/bson.ts
./node_modules/.deno/bson@6.3.0/node_modules/bson/src/bson_value.ts
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.bundle.js.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.cjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.cjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.mjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.mjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.bundle.js
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.rn.cjs.map
./node_modules/.deno/bson@6.3.0/node_modules/bson/lib/bson.rn.cjs
./node_modules/.deno/bson@6.3.0/node_modules/bson/bson.d.ts
It seems to download the dependencies just fine? 😵‍💫, at first I thought it didn't cache / download correctly, but that's probably not it. I can run any command inside the docker container on request! (hopefully with minimal delay)... what's going on? 🤔 Now I suspect there's something in the glue between when npm:mongodb asks for bson, and where it searches for the package on disk?
Deno AI Helper
Deno AI Helper11mo ago
It seems like you're encountering a module resolution issue with the mongodb npm package when running your Deno application inside a container. The error message indicates that the bson module, which is a dependency of mongodb, cannot be found. Here are a few steps you can take to troubleshoot and potentially resolve this issue: 1. Ensure Dependencies Are Installed: When you run your application locally, Deno caches the dependencies. However, in a container, you need to ensure that the dependencies are installed. You can do this by running deno cache on your dependencies before starting your application. This will download and compile the dependencies, including bson. 2. Check Import Map: You're using an import map to import mongodb. Make sure that the import map is correctly configured in your container environment and that Deno is aware of it when running the application. 3. Permissions: Since you're running with -A, which grants all permissions, this is unlikely to be a permissions issue. However, it's worth double-checking that the container has the necessary permissions to access the network and file system. 4. Deno Version: You've mentioned that you're using the same version of Deno in both places, which is good. Make sure that the Deno version in the container is indeed the same as the one on your local machine. 5. Container File System: There might be differences in how the file system is set up in the container compared to your local environment. Ensure that the paths where Deno expects to find the modules are correctly mapped in the container. 6. Deno MongoDB Driver: You mentioned that you tried using the Deno mongo package but encountered issues with load balancing. The documentation suggests using the official
Deno AI Helper
Deno AI Helper11mo ago
MongoDB driver with an npm: specifier, which you are already doing. However, if you want to give the Deno driver another try, make sure to use the latest version and check if there have been any updates or fixes regarding the load balancing issue. 7. Dockerfile Configuration: If you're using a Dockerfile to build your container image, ensure that it's set up correctly to work with Deno. This includes copying over the necessary files, setting up the correct working directory, and ensuring that the entry point or command is correct. 8. Debugging: Add more logging to your application to see if there are any other issues that might be causing the problem. You can also try running the container with an interactive shell and manually executing the Deno commands to see if you can replicate the issue. 9. Network Configuration: Since you're deploying to fly.io, make sure that the network configuration allows your container to reach the MongoDB instance. Network issues could prevent your application from connecting to the database. If you've gone through these steps and are still facing issues, you might want to consider reaching out to the Deno community or checking if there are any known
tegaki
tegakiOP11mo ago
SOLVED: starting a new fresh project (basically starting everything from scratch) seems to fix the issue, something in the repo was bad (config, code... not sure), causing the app to not work in containers :deno_it_works:
Bustin Base
Bustin Base11mo ago
The 3rd party module on x/mongo also works well, I have been using it successfully. https://deno.land/x/mongo/mod.ts

Did you find this page helpful?