This might be an obvious question, but I am pretty new to this.
I am trying to deploy an application with a simple mongodb integration. I wrote a Netlify function to retrieve data from it. I managed to get it to work perfectly on Dev (when launched from netlify-cli). However, when deployed, I get the following error in the function logs:
Apr 1, 02:40:26 PM: INIT_START Runtime Version: nodejs:18.v24 Runtime Version ARN: arn:aws:lambda:us-east-2::runtime:c09960ad0af4321e1a7cf013174f7c0d7169bf09af823ca2ad2f93c72ade708a
Apr 1, 02:40:26 PM: 2024-04-01T13:40:26.823Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'mongodb'\nRequire stack:\n- /var/task/functions/get_names.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'mongodb'","Require stack:","- /var/task/functions/get_names.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1087:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
Apr 1, 02:40:26 PM: INIT_REPORT Init Duration: 265.84 ms Phase: init Status: error Error Type: Runtime.ExitError
Apr 1, 02:40:27 PM: 2024-04-01T13:40:27.149Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'mongodb'\nRequire stack:\n- /var/task/functions/get_names.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'mongodb'","Require stack:","- /var/task/functions/get_names.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1087:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
Apr 1, 02:40:27 PM: INIT_REPORT Init Duration: 336.65 ms Phase: invoke Status: error Error Type: Runtime.ExitError
Apr 1, 02:40:27 PM: Unknown application error occurred
Runtime.ImportModuleError
Seems that the problem is the line at the very top of the function:
const { MongoClient } = require("mongodb");
I have tested a simple helloworld function, and that works correctly when deployed - so it seems I can call the functions correctly. The problem is the import.
I already have it in my package.json file
"dependencies": {
(...)
"mongodb": "^6.5.0",
(...)
},
How can I correctly import mongodb into the Netlify Function? What is missing?