node.bcrypt.js icon indicating copy to clipboard operation
node.bcrypt.js copied to clipboard

Application Crash with bcrypt's `compare` function on node:alpine

Open alissoncorsair opened this issue 1 year ago • 6 comments

Title:

Application Crash with bcrypt's compare function on node:alpine

Description:

I encountered an issue where my application crashes when using the compare function from bcrypt on the node:alpine Docker image. I tried switching to the latest Node.js version, which resolved the problem, but it persists on Alpine. Here are the details:

Steps to Reproduce:

  1. Use the node:alpine Docker image in a Node.js application.
  2. Utilize bcrypt's compare function to compare hashed passwords.
  3. Observe the application crash upon invoking the compare function.

Expected Behavior:

The application should successfully execute the compare function without crashing.

Actual Behavior:

The application crashes when calling the compare function, specifically on the node:alpine image.

Additional Details:

  • Dockerfile:
    FROM node:latest
    WORKDIR /usr/src/app
    COPY package*.json ./
    RUN npm install
    COPY . .
    RUN npm run build
    EXPOSE 3000
    CMD ["npm", "start"]
    
    
  • compose.yaml:
services:
  db:
    image: mongo
    container_name: mongo
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db
  app:
    build: .
    command: npm run dev
    ports:
      - "3000:3000"
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
      - /usr/src/app/dist
    depends_on:
      - db  
volumes:
  mongo_data:

alissoncorsair avatar Jun 10 '24 23:06 alissoncorsair

Hello, Same issue out there. Let me know if you find out why

loresclement avatar Jun 11 '24 09:06 loresclement

Usually happens due to a libc mismatch, check that the copy step isn't copying node_modules from the host into the container you just built

recrsn avatar Jun 11 '24 15:06 recrsn

Usually happens due to a libc mismatch, check that the copy step isn't copying node_modules from the host into the container you just built

Hello.

I have the same problem, this is my dockerfile:

FROM node

WORKDIR /home/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 4000

CMD ["npm", "run", `"dev"]

and this is my docker-compose.yml:

version: '3'

networks:
   local:

services:
   mysql:
      container_name: mysqldb
      image: mysql:8
      environment:
         MYSQL_DATABASE:
         MYSQL_USER:
         MYSQL_PASSWORD:
         MYSQL_ROOT_PASSWORD:
      networks:
         - local
      ports:
         - '3306:3306'

   app:
      depends_on:
         - mysql
      container_name: blog-api
      build:
         context: .
         dockerfile: Dockerfile
      environment:
         DB_HOST:
         DB_USERNAME:
         DB_PASSWORD:
         DB_DATABASE:
      volumes:
         - ./:/home/app
      ports:
         - '4000:4000'
      networks:
         - local

I tried using a .dockerignore file to ignore node_modules, but nothing changed, any suggestions?

I dont know if this changes anything, but I use WSL.

rubemarjrtech avatar Jul 04 '24 13:07 rubemarjrtech

same problem here node:20-alpine, any workaround please?

kopax avatar Sep 24 '24 14:09 kopax

same problem with hashSync() when built on node:20-alpine

stepskop avatar Oct 19 '24 10:10 stepskop

same problem with hashSync() when built on node:20-alpine

I suggest using older verison such as 5.10. It fixed my problem.

stepskop avatar Oct 21 '24 08:10 stepskop

Node.js v20.18.0 node:internal/modules/cjs/loader:1586 return process.dlopen(module, path.toNamespacedPath(filename)); ^

Error: Error loading shared library /node_modules/.pnpm/[email protected]/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: Exec format error at Module._extensions..node (node:internal/modules/cjs/loader:1586:18) at Module.load (node:internal/modules/cjs/loader:1288:32) at Module._load (node:internal/modules/cjs/loader:1104:12) at Module.require (node:internal/modules/cjs/loader:1311:19) at require (node:internal/modules/helpers:179:18) at Object. (/node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js:6:16) at Module._compile (node:internal/modules/cjs/loader:1469:14) at Module._extensions..js (node:internal/modules/cjs/loader:1548:10) at Module.load (node:internal/modules/cjs/loader:1288:32) at Module._load (node:internal/modules/cjs/loader:1104:12) { code: 'ERR_DLOPEN_FAILED' }

marcephrem avatar Oct 30 '24 18:10 marcephrem

Node.js v20.18.0 node:internal/modules/cjs/loader:1586 return process.dlopen(module, path.toNamespacedPath(filename)); ^

Error: Error loading shared library /node_modules/.pnpm/[email protected]/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: Exec format error at Module._extensions..node (node:internal/modules/cjs/loader:1586:18) at Module.load (node:internal/modules/cjs/loader:1288:32) at Module._load (node:internal/modules/cjs/loader:1104:12) at Module.require (node:internal/modules/cjs/loader:1311:19) at require (node:internal/modules/helpers:179:18) at Object. (/node_modules/.pnpm/[email protected]/node_modules/bcrypt/bcrypt.js:6:16) at Module._compile (node:internal/modules/cjs/loader:1469:14) at Module._extensions..js (node:internal/modules/cjs/loader:1548:10) at Module.load (node:internal/modules/cjs/loader:1288:32) at Module._load (node:internal/modules/cjs/loader:1104:12) { code: 'ERR_DLOPEN_FAILED' }

same problem

marcephrem avatar Oct 30 '24 18:10 marcephrem

It took me over 2 hours to fix this error 😖 @marcephrem @alissoncorsair

I have fixed it when I add it this line inside dockerFile compose

RUN npm rebuild bcrypt

after this line

RUN npm install

And It's worked and fixed 🥳🎉🎊

MohammedAliBinTaleb avatar Feb 04 '25 14:02 MohammedAliBinTaleb

In my experience bcrypt in the alpine image is very unstable and behaves unpredictable throwing errors here and there. I got Segmentation fault (core dumped) and after a few days of attempts to fix this eventually switched to the node:slim image which is although a bit larger, much more stable.

andreyponomarevru avatar Feb 13 '25 11:02 andreyponomarevru