Cannot find module 'bcrypt' error in Function

Hey,

I have this site: https://elegant-madeleine-866344.netlify.app/
I have created a function which imports “bcrypt” module.
When testing locally with “netlify functions:serve” my function works ok.
But the deployed function doesn’t work, giving an error: “Cannot find module ‘bcrypt’”

Here are the logs from the Function in Netlify:

Sep 13, 07:24:02 PM: INIT_START Runtime Version: nodejs:18.v12	Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:0bdff101a7b4e0589af824f244deb93200e4663c2a8d7d0148b76cd00c48777aSep 13, 07:24:02 PM: 2023-09-13T16:24:02.653Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'bcrypt'\nRequire stack:\n- /var/task/netlify/functions/auth.js\n- /var/task/auth.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'bcrypt'","Require stack:","- /var/task/netlify/functions/auth.js","- /var/task/auth.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1061:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)","    at async start (file:///var/runtime/index.mjs:1256:23)","    at async file:///var/runtime/index.mjs:1262:1"]}Sep 13, 07:24:02 PM: 2023-09-13T16:24:02.867Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'bcrypt'\nRequire stack:\n- /var/task/netlify/functions/auth.js\n- /var/task/auth.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'bcrypt'","Require stack:","- /var/task/netlify/functions/auth.js","- /var/task/auth.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1061:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)","    at async start (file:///var/runtime/index.mjs:1256:23)","    at async file:///var/runtime/index.mjs:1262:1"]}Sep 13, 07:24:02 PM: Unknown application error occurred
Runtime.ImportModuleError

Here is my bcrypt version:

"bcrypt": "^5.1.1",

And here is the Network error logged in my Browser

I have tried also to remove “bcrypt” and import another module, (“moment”), to see whether this is a generic issue for every module. But “moment” module worked ok.

Any ideas how to fix this?

Best,
Panos

Hi @rapanaki thanks for the post.
Since Netlify functions run in a serverless environment, trying to install bcrypt has to also install the native dependency node-gyp which might not install in a serverless environment.

An alternative will be to install and use bcryptjs
The bcryptjs package is an optimized bcrypt in JavaScript with zero dependencies

Make sure you remove the bcrypt package first and then install the bcryptjs package

npm uninstall bcrypt

npm install bcryptjs

Kindly make the changes and then let me know the outcome.

Thanks.

Hi @clarnx ,

It worked with bcryptjs

Thank you!

1 Like

thanks for confirming you found your solution!

1 Like