serverless-plugin-optimize icon indicating copy to clipboard operation
serverless-plugin-optimize copied to clipboard

"Error: Cannot find module './drivers/node-mongodb-native/connection'" when used with optimize

Open sebastiangug opened this issue 6 years ago • 5 comments

Stackoverflow thread here.

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.

sebastiangug avatar Aug 30 '19 12:08 sebastiangug

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.

sebastiangug avatar Aug 30 '19 12:08 sebastiangug

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?

sebastiangug avatar Aug 30 '19 15:08 sebastiangug

any update on this?

maazmk avatar Jul 30 '20 14:07 maazmk

Yeah, the update is to stop using serverless for deploying any kind of serious infrastructure

sebastiangug avatar Jul 30 '20 14:07 sebastiangug

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

dhiraj-indusved avatar Apr 28 '21 18:04 dhiraj-indusved