I have a serverless REST api connecting to MongoDB atlas. When making changes and running sls deploy
everything deploys as normal. But when I query the DB from postman, I was getting an error. The cloudwatch logs were as follows:
"errorType": "Error",
"errorMessage": "/var/task/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header",
"stack": [
"Error: /var/task/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header",
" at Object.Module._extensions..node (internal/modules/cjs/loader.js:1057:18)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Module.require (internal/modules/cjs/loader.js:887:19)",
" at require (internal/modules/cjs/helpers.js:74:18)",
" at Object.<anonymous> (/var/task/node_modules/bcrypt/bcrypt.js:6:16)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)"
]
A developer took the same exact code ran sls deploy
from his computer (Ubuntu) and everything works. I was able to send request and get results from the DB.
I tried npm uninstall bcrypt
and npm i bcrypt
then sls deploy again
and still received the invalid ELF header error
.
The issue is apparently the M1 MacBook. Is there a work around to handling this issue? I really don't want to rely on dev's running sls deploy
every time I want to make changes to the API.
I solved this by using bcryptjs (npm i bcryptjs) instead of bcrypt package.
after install use it like this in the code const bcrypt = require('bcryptjs');
It appears bcrypt fails on lambda because it uses node-gyp to build and install - not sure though will have to work though it.
Until then, bcryptjs works.