Build failed when using target: 'serverless' with mongodb

Hi,

When I tried to build a project with

  • target: 'serverless' with mongodb package, I got this error:
    ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb-client-encryption' in '/opt/build/repo/node_modules/mongodb/lib'
  • target: 'server', everything work fine but we landed on Page Not Found
  1. Why do we need to set target: 'serverless'?
  2. How can we solve ModuleNotFoundError: Module not found: Error: Can't resolve 'mongodb-client-encryption' in '/opt/build/repo/node_modules/mongodb/lib when using target: 'serverless'? (I tried CI= npm run build but still got the same error)
/my_folder
  /pages
    /api
      test.js
    index.js
  next.config.js
  package.json
// pages/api/test.js
import { MongoClient, ObjectID } from 'mongodb'
export default async function handler(req, res) {
  res.send('ok')
}
// pages/index.js
export default function Home() {
  return <h1>Hi</h1>
}
// next.config.js
const configs = {
  target: 'serverless',
}
module.exports = configs
{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "package": "next build && next export",
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "mongodb": "^3.6.8",
    "next": "^10.2.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  },
  "devDependencies": {},
  "peerOptionalDependencies": {}
}

Thank you,

Try installing this module: mongodb-client-encryption - npm

1 Like

I tried to install mongodb-client-encryption but it keep asking me to install another one and so on and I don’t know how deep it is.
installed mongodb-client-encryption
then aws4 because ModuleNotFoundError: Module not found: Error: Can't resolve 'aws4' in '/opt/build/repo/node_modules/mongodb/lib/core/auth'
then a bunch of other package if you keep installing and build: hdb-pool, mysql, mysql2, oracledb, pg
I installed until I was not able to install pg-native and forgot the reason but the fact it keep telling there are missing module I don’t kknow when it will end.

That’s a strange problem. Sadly, I’m not familiar with NextJS or MongoDB for that matter. You can try asking on Mongo forums till then, or you can continue to wait. Meanwhile if you do get this solved, kindly post your solution.

The solution I found is target: 'server' as I mentioned in my first post but I got Page Not Found when trying to see the index webpage: maybe because it is not target: 'serverless'.

Could you share your repo?

All the code you need to test is in the first post with the file/folder structure.
I wrote simple code to test to be sure that the issue is about mongodb and target: 'serverless'.

Hey there, @elbopha :wave:

Looks like this thread has been a bit quiet since last week. Are you still experiencing difficulties? If so, please let us know what additional debugging steps you have taken since last week so that we can look into this further.

Thanks!