How can I bundle MONGODB?

373 Views Asked by At

I'm try to bundle with browserify when I try

browserify index.js > main.js

I get this error

Error: Can't walk dependency graph: Cannot find module 'mongodb-client-encryption' from '/home/xlrnz/Documenti/simple-crud/node_modules/mongodb/lib/operations/connect.js'
    required by /home/xlrnz/Documenti/simple-crud/node_modules/mongodb/lib/operations/connect.js
    at /usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:116:35
    at processDirs (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:269:39)
    at isdir (/usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:276:32)
    at /usr/lib/node_modules/browserify/node_modules/resolve/lib/async.js:25:69
    at FSReqCallback.oncomplete (node:fs:194:21)

I can't understand. This is index.js:

const dotenv = require("dotenv").config();

const monk = require("monk");

const db = monk(
  `mongodb+srv://simple-crud-xlrnz:${process.env.MONGO_DB_KEY}@simple-crud.jrjfl.mongodb.net/simple-crud-DB?retryWrites=true&w=majority`
);

and this is package-json:

{
  "name": "simple-crud",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "dotenv": "^8.2.0",
    "nodemon": "^2.0.7"
  },
  "dependencies": {
    "monk": "^7.3.3"
  }
}

I'm stuck here for about 2hours anyone can help?

1

There are 1 best solutions below

0
On BEST ANSWER

The client encryption module being referenced in your error message requires spawning a process which cannot be done from the browser. I am not very familiar with browserification but if you can configure it to ignore this module you might succeed with browserifying.

With that said, as pointed out in the comments, generally nobody would connect directly to a MongoDB server from the browser. The closest sensible alternative is MongoDB Realm/Stitch. So the thing you are trying to do is basically not supported.

There are many MongoDB tutorials out there, I suggest following one of them.