bcrypt invalid elf header when running node app
Asked Answered
E

15

134

I'm working on a nodejs project for school. I wasn't able to install bcrypt with npm so i installed bcrypt-nodejs and the project worked fine yesterday. But today, when I do a "node app" i have this error :

/.../node_modules/bcrypt/node_modules/bindings/bindings.js:79
        throw e
              ^
Error: /.../node_modules/bcrypt/build/Release/bcrypt_lib.node: invalid ELF header
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at bindings (/.../node_modules/bcrypt/node_modules/bindings/bindings.js:74:15)
    at Object.<anonymous> (/.../node_modules/bcrypt/bcrypt.js:1:97)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)

my package.json file looks like this:

{
  "name": "Supinfarm",
  "version": "0.0.0",
  "env": {
              "PYTHON": "/usr/bin/python2.6"
        },
  "dependencies": {
    "express": "3.1.0",
    "connect-flash": "*",
    "jade": "*",
    "stylus": "*",
    "passport": "*",
    "passport-local": "*",
    "mongoose": "*",
    "bcrypt": "*"
  }
}

I'm on Linux ubuntu 10.04 LTS I've tried to find a solution on google without success... Can somebody help me?

Enenstein answered 4/4, 2013 at 11:15 Comment(1)
yes, i installed ubuntu 12.04 and i was able to install and use bcrypt. thanks for you interest in my issue.Enenstein
A
207

I've found that bcrypt compiled on OSX will not quite work on Linux. In other words, if you check in the bcrypt compiled on your local OSX workstation, and try to run the node app on your linux servers, you will see the error above.

Solution: npm install bcrypt on Linux, check that in, solved.

Probably the best way to deal with this is exclude your node_modules in .gitignore... and npm install remotely.

Antakiya answered 15/12, 2013 at 1:30 Comment(8)
That's because they're different operating systems, and quite possibly, different underlying processor architectures. When I was in college we had two UNIX clusters: one running on a VAX the other on an Alpha. CS projects HAD to be compiled on the VAX since that's what the professor used...Transfusion
@Transfusion Sure, but npm modules cross-compile: installing somthing with a binary component gives you a Mach (OS X), ELF (Linux) and PXE (Windows) binary.Lesbianism
Only problem is: bcrypt, unlike other node modules, only installs a single OS binary. So committing a Linux-installed bcrypt will break your Mac developer systems, since node_modules/bcrypt/build/Release/bcrypt_lib.node is now a Linux binary. Run file /Users/mikemaccana/Documents/sandpitlab/waves/node_modules/bcrypt/build/Release/bcrypt_lib.node to test.Lesbianism
@Lesbianism they certainly do not. we use vmware & ubuntu for dev, but shared with our macs. socket.io, leveldb, phantomsj, etc all compile for the target architecture to which you're installing. If I install level on my mac and try to use it from the VM, it completely fails since it's compiled for darwin and not linux.Transfusion
@Transfusion leveldb may not, but many modules do. See evidence in #24159732 for an example and try it yourself.Lesbianism
@Lesbianism node-sass only works because (from the readme.md): Node-sass includes pre-compiled binaries for popular platforms, to add a binary for your platform follow these steps:. It is not cross-compiling, rather giving you pre-compiled binaries. Node-gyp does NOT cross-compile by default.Transfusion
Thanks @tkone. I guess I picked a bad module to check !Lesbianism
spent a whole day and finally found this answer. I can't understand why OSX bcrypt node_modules is different than LINUX's bcrypt node_module. It's so cumbersome to do: "rm -r node_modules" and "npm i" everytime after uploading onto AzureCason
H
43

If you are running inside a docker container as I am, all you need is a .dockerignore with 'node_modules' specified in it.

Some libraries need to be compiled on the host machine and therefore your modules can be stale.

Humiliating answered 25/10, 2019 at 15:42 Comment(3)
This was my problem. bcrypt was built on MacOS X but running it in a linux container.Deoxidize
Really thankful to you for this, was breaking my head for long enough.Refractor
This was save my time. Thank you.Charlean
G
33

My issue was with my docker-compose.yml file, I already had node_modules in my .dockerignore but I also needed to add the node_modules directory as a volume:

volumes:
  - ./:/usr/src/app
  - /usr/src/app/node_modules

Grassofparnassus answered 12/2, 2020 at 17:33 Comment(5)
Not sure why, but only this solution worked for me! Thanks!Bono
I use Windows with contianer node:bind, and only this solution work for me.Spume
Same for me, im using windows with Docker and don´t know why, but this solution worked for me too. ThanksPleonasm
NOT windows and worked for me, thanks. I already had my node_modules checked in like youDrink
this works for me in windows. thank you.Legge
G
21

There is a simple way that allowed me to solve this problem:

1. Uninstall bcrypt

npm uninstall bcrypt

2.- Install bcrypt again

 npm i bcrypt

The error occurs because when you install bcypt, npm installs the recommended version for your machine and operating system, but when you are on another machine, this doesn't work.

-------- UPDATE ----------------------------------------

It also seems to me that another solution which is to grant root permissions to bcrypt installation, it happens because bcryp uses its own user but it has no permissions, so:

1. You must grant root permission to your project folder. go outside of your project folder and then

sudo su

Then enter your root password to get root user permissions

2. Grant permission to your project folder

chmod -R 777 <project_folder>

3. Go to your project folder and install bcrypt

cd <project_folder>

AND

npm i bcrypt --unsafe-perm=true --allow-root --save

Ready, if everything was OK, your bcrypt module will install without problems.

Ghassan answered 18/6, 2020 at 7:31 Comment(1)
The uninstall and re-install worked for me when a force flag wasn't.Fredkin
O
17

I came across the same problem. I deployed by code in AWS Lambda using windows. I got the same error. I fixed it using 'bcryptjs' npm library.

npm install bcryptjs

Overboard answered 1/7, 2021 at 4:46 Comment(0)
J
10

change package to:

npm install bcryptjs
var bcrypt = require('bcryptjs');

The functions are the same.

Get more information here https://github.com/dcodeIO/bcrypt.js

Jaejaeger answered 19/6, 2021 at 21:34 Comment(0)
T
9

I was also facing the same issue with bcrypt v.1.0.3. Just updated to the latest version (3.0.1) and its working fine now

Run

npm install bcrypt@latest --save
Tarragona answered 17/10, 2018 at 12:13 Comment(3)
This solved the issue, thank you, (I'm programming on MacOS and deploying it on Ubuntu server !)Magnetoelectricity
This doesn't work for me (I'm using v3.0.4) in MacOSSlap
This doesn't work for me and I'm using v5.0.0 in MacOSCusec
G
6

To solve this problem for a Docker container.

You can create a .dockerignore with this configuration :

node_modules/
dist/
Girlie answered 3/7, 2022 at 20:8 Comment(0)
E
5

in my case I was using nodejs in windows, when I use docker (with linux) I got that error, add the .dockerignore file with the folder node_modules and with this I work correctly, the nodejs packages in windows load differently in linux, so it is better that you install them from scratch in linux.

Effort answered 3/9, 2021 at 4:21 Comment(0)
S
3

If you are in Docker I resolve the issue by logging in to the maching running my service and running npm uninstall bcrypt and then npm install bcrypt

Susann answered 6/2, 2021 at 12:28 Comment(0)
O
1

First make sure you are not uploading the node modules and are running npm install on your linux machine itself as bcrypt installation can vary depending on the platform you use. You can look at other installation instructions for other platforms below.
https://github.com/kelektiv/node.bcrypt.js/wiki/Installation-Instructions

If you are having further problems it could be related to node-pre-gyp. A dependency of bcrypt.

For AWS Elastic Beanstalk When deploying to Elastic Beanstalk running Node 8.x, node-gyp doesn't have sufficient permissions to write to the tmp directory. bcrypt won't install and the application deployment will fail.

A workaround is to add a .npmrc file to the root of your project that will force node-gyp to run as root and allow the installation to complete. File contents for .npmrc:

# Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5 or @6
unsafe-perm=true

Another alternative (Perhaps the more right way) is to make .ebextensions file with code:

.ebextensions:00_change_npm_permissions.config:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/00_set_tmp_permissions.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      chown -R nodejs:nodejs /tmp/.npm

This will give you sufficient access to run node-gyp

Oeuvre answered 18/6, 2020 at 4:5 Comment(0)
H
0

For those deploying an app to AWS elastic beanstalk, and gonna install bcrypt on the server, include in a post deploy hook in .ebextensions/01_build.config:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_build_app.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      cd /var/app/current/
      rm -rf node_modules/bcrypt
      sudo /opt/elasticbeanstalk/node-install/node-v10.13.0-linux-x64/bin/npm install bcrypt@latest
Heiskell answered 17/12, 2018 at 2:20 Comment(0)
F
0

may be a difference in architecture, using the google compute engine i run this on my google cli putty or google terminal:

npm uninstall bcrypt

then

npm i bcrypt

this should fix it

Footie answered 15/7, 2023 at 9:57 Comment(0)
P
-1

I know that this might be a bit of a hassle but it is a solution. What I did when I needed to implement Bcrypt, was I started a Cloud 9 instance. For those of you who don't know, Cloud9 is a basic AWS IDE that runs on an EC2 instance. From Cloud9 you are able to upload your code on the ide as a lambda function. So I wrote the function on Cloud9 and when I uploaded it, the code worked.

Potentiality answered 13/7, 2020 at 22:5 Comment(0)
S
-2

I usually use macOS with Docker, to add some packages I first go into the Docker server docker exec -it nameServer /bin/bash and then add the package npm install bcrypt for example. So I guarantee everything will go run on the production server

Sarette answered 8/7, 2021 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.