serverless-plugin-optimize
serverless-plugin-optimize copied to clipboard
"Error: Cannot find module './drivers/node-mongodb-native/connection'" when used with optimize
I've also added mongoose as a separate layer with all of its dependencies, it throws the exact same error. Is there something special I have to config somewhere to get this to work?
On the list of third-party modules this is supposed to work with, mongoose must be quite high as it is the most popular choice for mongodb via node.
I've just bundled my app with the serverless-plugin-monorepo and it works, but my bundle is 40mb instead of 2 so that's not cool. If I exclude 'mongoose' using the custom -> optimizer -> exclude: ['mongoose'] I don't get that error but I do get the following error:
"errorType": "Error",
"errorMessage": "ENOENT: no such file or directory,
scandir '/var/task/_optimize/tlabs-api-dev-main/src/node_modules/pkginfo'",
"code": "ENOENT",
"stack": [
Am i correct to assume that package is not being bundled correctly with the whole thing? I couldn't find any reason why that dependency is even there though.
Managed to exclude mongoose successfully. The second error was for a crappy dependency of another package, I changed that package with something else that doesn't have those dependencies, now getting:
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module './stores/memory'",
Is this really optimizing anything at all if I have to manually remove a bunch of dependencies that break the whole thing?
any update on this?
Yeah, the update is to stop using serverless for deploying any kind of serious infrastructure
Anyone coming here for Error: Cannot find module './drivers/node-mongodb-native/connection
I found a solution here https://github.com/Automattic/mongoose/issues/9603 , thanks to hugdru
The problem is some parts of mongoose are required dynamically using a variable. One can use https://www.npmjs.com/package/patch-package to create a temporary patch until the next major release of mongoose where this is going to be fixed.
Changes to be made in node_modules/mongoose/lib/index.js with the changes given here https://gist.github.com/hugdru/982a31d929a030e29f852c4bfafd9b16 and the. just run yarn patch-package mongoose