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 πŸ€”
T
tegakiβ€’64d 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?
DAH
Deno AI Helperβ€’64d 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
DAH
Deno AI Helperβ€’64d 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
T
tegakiβ€’64d 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:
BB
Bustin Baseβ€’64d ago
The 3rd party module on x/mongo also works well, I have been using it successfully. https://deno.land/x/mongo/mod.ts
More Posts
TC39 stage 3 decorators not working anymore in Deno DeployAs of Feb 15th, deployments to #deployβ„’ fail when using TC39 decorators. Despite the decision to suUse Deno permissions in your own CLIIf I create a CLI with Deno, can I somehow reuse or pass on the permissions? ```bash > mycli --alloHas someone worked on a win32/x11/mac native window creation using FFI?Using only operating system native libraries? No SDL, etc.How to unset white background of SVG in Jupyter notebook?I’ve got a Jupyter notebook with Deno kernel in VSCode that displays an SVG. The SVG has a forced whHow to get the Docker ARM image for DenoI keep getting this error when trying to deploy my code: ``` WARNING: The requested image's platfoDeno compiled bin not create fileHi, I have a script that create file with Deno.writeFileSync() on current location When run script VSCode Extension Failing ot Initialize:party_deno: **RESOLVED** :party_deno: Having a bit of trouble using the VSCode Extension. `deno`deno command stdin, stdout continous readingI am trying to spawn an AI (cpp code) however after launching the code it immediately exits. It alsoProblem rerender a island when updating the value of a signal!Hi there im trying to make a visual change to the selected category button. However it does not rereCannot find namespace NodeJSHi Context: using deno with npm lib (imap). There seems to be a missing/unresolved to NodeJS that Which components exactly should be islands?Hi! I am currently developing a web app using fresh and I am getting confused about the usage of islError using npm:ssh2-sftp-client@10.0.3I am trying to connect to a sftp server using the library mentioned in the title (that is a wrapper How to restrict global scope in dynamically created function?I want to dynamically create a function using Function() constructor. How can I restrict access to gTyping for Dynamic importsSo I have a function like this that uses a dynamic import like ```ts export async function func1(im