I had this issue only when working with Docker and Nestjs, above solutions didn't worked. This one eventually helped:
in package.json
:
"pnpm": {
"onlyBuiltDependencies": ["bcrypt"]
},
in Dockerfile:
RUN pnpm install
RUN pnpm rebuild bcrypt