Mongodb Error when deploying express app to Netlify - Can't resolve 'mongodb-client-encryption'

When deploying my express/mongoDB app to netlify I keep running in the error ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb-client-encryption' in '/opt/build/repo/node_modules/mongodb/lib' this runs fine locally when running ‘yarn start’ but ‘yarn build’ I get the same error above.

url:
https://pedantic-hermann-961116.netlify.app/

Directory Setup:

config/Db.js:

const mongoose = require('mongoose');
const config = require('config');
const db = config.get('mongoURI');

const connectDB = async () => {
  try {
    await mongoose.connect(db, {
      useNewUrlParser: true
    });

    console.log('MongoDB Connected...');
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
};

module.exports = connectDB;

config/default.js:

{
  "mongoURI": "mongodb+srv://User:Pass@cluster0.eqzjk3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
  "baseUrl": "http://localhost:5000"
}

models/Url.js

const mongoose = require('mongoose');

const urlSchema = new mongoose.Schema({
  urlCode: String,
  longUrl: String,
  shortUrl: String,
  date: { type: String, default: Date.now }
});

module.exports = mongoose.model('Url', urlSchema);

src.js:

const express = require("express");
const serverless = require("serverless-http");
const connectDB = require('../config/db');
const Url = require('../models/Url');
const validUrl = require('valid-url');
const shortid = require('shortid');
const config = require('config');

const app = express();
const router = express.Router();

// Connect to database
connectDB();

router.get('/:code', async (req, res) => {
  try {
    const url = await Url.findOne({ urlCode: req.params.code });

    if (url) {
      return res.redirect(url.longUrl);
    } else {
      return res.status(404).json('No url found');
    }
  } catch (err) {
    console.error(err);
    res.status(500).json('Server error');
  }
});

router.post('/shorten', async (req, res) => {
  const { longUrl } = req.body;
  const baseUrl = config.get('baseUrl');

  // Check base url
  if (!validUrl.isUri(baseUrl)) {
    return res.status(401).json('Invalid base url');
  }

  // Create url code
  const urlCode = shortid.generate();

  // Check long url
  if (validUrl.isUri(longUrl)) {
    try {
      let url = await Url.findOne({ longUrl });

      if (url) {
        res.json(url);
      } else {
        const shortUrl = baseUrl + '/' + urlCode;

        url = new Url({
          longUrl,
          shortUrl,
          urlCode,
          date: new Date()
        });

        await url.save();

        res.json(url);
      }
    } catch (err) {
      console.error(err);
      res.status(500).json('Server error');
    }
  } else {
    res.status(401).json('Invalid long url');
  }
});


app.use(express.json());
// Define Routes
app.use('/', router);
app.use('/api/url', router);

const PORT = 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

module.exports = app;
module.exports.handler = serverless(app);

netlify.toml:

[build]
  functions = "functions"

[dev]
  publish = "dist"

Do you have that in your package.json?

1 Like


This is the error message I get when to login using the front end IT WILL Not connect to back end MonoDB
This was working in Node 12 but the upgrade to node16 have disabled it What I can do to fix this. Thank for all your help in advance The netilfy app is running at https://epic-kare-a20d74.netlify.app this is the FRON end App same as nc.healthparameters.com online and the back end it at https://jolly-noether-6acd8e.netlify.app This is MonoDB online

It simply means your data object doesn’t have a property called msg. You might want to log the value of data to see what you’re getting.