简体   繁体   English

运行节点应用程序时bcrypt无效的elf标头

[英]bcrypt invalid elf header when running node app

I'm working on a nodejs project for school.我正在为学校开发一个 nodejs 项目。 I wasn't able to install bcrypt with npm so i installed bcrypt-nodejs and the project worked fine yesterday.我无法使用 npm 安装 bcrypt,所以我安装了 bcrypt-nodejs,并且该项目昨天运行良好。 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:我的 package.json 文件如下所示:

{
  "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?我在 Linux ubuntu 10.04 LTS 我试图在谷歌上找到一个没有成功的解决方案......有人可以帮助我吗?

I've found that bcrypt compiled on OSX will not quite work on Linux .我发现在 OSX 上编译的 bcrypt 在 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.换句话说,如果您签入在本地 OSX 工作站上编译的 bcrypt,并尝试在您的 linux 服务器上运行节点应用程序,您将看到上述错误。

Solution: npm install bcrypt on Linux, check that in, solved.解决方案:在 Linux 上npm install bcrypt ,签入,解决。

Probably the best way to deal with this is exclude your node_modules in .gitignore... and npm install remotely.处理这个问题的最好方法可能是在 .gitignore... 中排除你的 node_modules 并远程安装 npm。

If you are running inside a docker container as I am, all you need is a .dockerignore with 'node_modules' specified in it.如果您像我一样在 docker 容器中运行,您所需要的只是一个 .dockerignore,其中指定了“node_modules”。

Some libraries need to be compiled on the host machine and therefore your modules can be stale.一些库需要在主机上编译,因此您的模块可能是陈旧的。

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:我的问题是我的 docker-compose.yml 文件,我的 .dockerignore 中已经有 node_modules 但我还需要将 node_modules 目录添加为卷:

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

There is a simple way that allowed me to solve this problem:有一种简单的方法可以让我解决这个问题:

1. Uninstall bcrypt 1.卸载bcrypt

npm uninstall bcrypt

2.- Install bcrypt again 2.- 再次安装 bcrypt

 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.出现错误是因为当你安装 bcypt 时,npm 会为你的机器和操作系统安装推荐的版本,但是当你在另一台机器上时,这不起作用。

-------- 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:在我看来,另一个解决方案是授予 bcrypt 安装 root 权限,这是因为 bcryp 使用它自己的用户但它没有权限,所以:

1. You must grant root permission to your project folder. 1.您必须授予项目文件夹的root权限。 go outside of your project folder and then走出你的项目文件夹,然后

sudo su

Then enter your root password to get root user permissions然后输入你的root密码获取root用户权限

2. Grant permission to your project folder 2. 授予您项目文件夹的权限

chmod -R 777 <project_folder>

3. Go to your project folder and install bcrypt 3. 转到您的项目文件夹并安装 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.准备好了,如果一切正常,您的 bcrypt 模块将顺利安装。

I was also facing the same issue with bcrypt v.1.0.3.我也面临与 bcrypt v.1.0.3 相同的问题。 Just updated to the latest version (3.0.1) and its working fine now刚刚更新到最新版本(3.0.1),现在工作正常

Run

npm install bcrypt@latest --save

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.在我的情况下,我在 windows 中使用 nodejs ,当我使用 docker (with linux) 时出现该错误,添加带有node_modules文件夹的 .dockerignore 文件,这样我可以正常工作,windows 中的 nodejs 包在 linux 中加载不同,所以最好在 linux 中从头开始安装它们。

I came across the same problem.我遇到了同样的问题。 I deployed by code in AWS Lambda using windows.我使用 windows 在 AWS Lambda 中通过代码部署。 I got the same error.我得到了同样的错误。 I fixed it using 'bcryptjs' npm library.我使用'bcryptjs' npm 库修复了它。

npm install bcryptjs

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.首先确保您没有上传节点模块并且在您的 linux 机器上运行 npm install,因为 bcrypt 安装可能会因您使用的平台而异。 You can look at other installation instructions for other platforms below.您可以在下面查看其他平台的其他安装说明。
https://github.com/kelektiv/node.bcrypt.js/wiki/Installation-Instructions https://github.com/kelektiv/node.bcrypt.js/wiki/Installation-Instructions

If you are having further problems it could be related to node-pre-gyp.如果您还有其他问题,则可能与 node-pre-gyp 有关。 A dependency of bcrypt. 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.对于 AWS Elastic Beanstalk 部署到运行 Node 8.x 的 Elastic Beanstalk 时,node-gyp 没有足够的权限写入 tmp 目录。 bcrypt won't install and the application deployment will fail. bcrypt 不会安装,应用程序部署将失败。

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.一种解决方法是将 .npmrc 文件添加到项目的根目录,这将强制 node-gyp 以根用户身份运行并允许安装完成。 File contents for .npmrc: .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 文件:

.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这将使您有足够的权限来运行 node-gyp

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如果您在 Docker 中,我通过登录运行我的服务并运行npm uninstall bcrypt然后npm install bcrypt来解决问题

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 :对于那些将应用程序部署到 AWS 弹性 beanstalk 并在服务器上安装 bcrypt 的用户,请在.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

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.当我需要实现 Bcrypt 时,我所做的是启动了一个 Cloud 9 实例。 For those of you who don't know, Cloud9 is a basic AWS IDE that runs on an EC2 instance.对于那些不知道的人,Cloud9 是一个在 EC2 实例上运行的基本 AWS IDE。 From Cloud9 you are able to upload your code on the ide as a lambda function.从 Cloud9,您可以将代码作为 lambda 函数上传到 ide。 So I wrote the function on Cloud9 and when I uploaded it, the code worked.所以我在 Cloud9 上写了这个函数,当我上传它时,代码就可以工作了。

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在此处获取更多信息https://github.com/dcodeIO/bcrypt.js

To solve this problem for a Docker container.为 Docker 容器解决此问题。

You can create a .dockerignore with this configuration :您可以使用以下配置创建.dockerignore

node_modules/
dist/

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.我通常使用 macOS 和 Docker,添加一些包我首先进入 Docker 服务器docker exec -it nameServer /bin/bash然后添加包npm install bcrypt例如。 So I guarantee everything will go run on the production server所以我保证一切都会在生产服务器上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM