Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is importing bcrypt causing a "Cannot find module napi-v3/bcrypt_lib.node" error?

Using the Javascript bcrypt package, I get this server-side runtime import error only when deployed (on Vercel.com using Next.js /api routes which are AWS Lambdas under the hood):

41f80   ERROR   Error: Cannot find module '/var/task/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node'
Require stack:
- /var/task/node_modules/bcrypt/bcrypt.js
- /var/task/.next/serverless/pages/api/login.js
- /var/task/___next_launcher.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/var/task/node_modules/bcrypt/bcrypt.js:6:16)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/bcrypt/bcrypt.js',
    '/var/task/.next/serverless/pages/api/login.js',
    '/var/task/___next_launcher.js',
    '/var/runtime/UserFunction.js',
    '/var/runtime/index.js'
  ]
}

I definitely npm installed it:

"bcrypt": "^5.0.1",

It works locally, but not when deployed.
I'm using the library server-side.
I've scoured the internet for hours trying to find the answer to this, but I can't find anyone who's has this exact issue.

Local node version: v14.16.0
Hosted node version: v14x

like image 455
Kaelin Avatar asked Dec 30 '22 15:12

Kaelin


1 Answers

Found the answer here: https://forum.serverless.com/t/after-making-a-deploy-i-test-the-function-and-show-this/8624/2

TLDR: Had to use bcryptjs which is written purely in Javascript. Apparently the regular bcrypt library requires some C++ dependencies that aren't gauranteed in some environments (such as AWS' node environment).

like image 87
Kaelin Avatar answered May 08 '23 08:05

Kaelin