Beanstalk:Node.js部署 - 由于权限被拒绝,node-gyp失败

Jak*_*olý 61 node.js amazon-elastic-beanstalk

将Node.js应用程序(节点6,npm 5)部署到Beanstalk失败了:

gyp ERR!堆栈错误:EACCES:权限被拒绝,mkdir'/ tmp/deployment/application/node_modules/heapdump/build'

虽然错误不是特定于包,但任何node-gyp调用都会失败.

AWS控制台中的ERROR事件显示:

[实例:i-12345]命令在实例上失败.返回码:1输出:(TRUNCATED).../opt/elasticbeanstalk/containerfiles/ebnode.py",第180行,在npm_install中提升e subprocess.CalledProcessError:命令'['/ opt/elasticbeanstalk/node-install/node- v6.10.0-linux-x64/bin/npm',' - production','install']'返回非零退出状态1.挂钩/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh失败.更多详细信息,请使用控制台或EB CLI检查/var/log/eb-activity.log.

eb-activity.log包含上述npm错误.

通过上载不包含的.zip文件手动部署应用程序node_modules.即它不是通过eb命令行工具部署的.

Jak*_*olý 174

解决方案是将文件添加.npmrc到具有以下内容的应用程序:

# Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5
unsafe-perm=true
Run Code Online (Sandbox Code Playgroud)

(或配置NPM所以在任何其他方式.(虽然设置npm_config_unsafe_perm=true/opt/elasticbeanstalk/env.vars我没有工作.)

说明

npm install由root用户运行,但node-gyp它为某些包触发的进程由默认用户运行ec2-user.该用户无法访问/tmp/deployment/application/node_modules/由npm安装运行创建并由root拥有的目录.(它可能也无法获得/tmp/.npm/tmp/.config由同一个创建的.)通过使unsafe-perm我们强迫NPM也运行节点GYP为根,以避免这个问题.

(就个人而言,我宁愿全部运行ec2-user而不是,root但我想这会更多涉及:-))

积分

unreal0指出了我的解决方案

相关问题

  • 谢谢!`.npmrc`中的`unsafe-perm = true`解决了我的问题. (11认同)
  • 这个解决方案对我不起作用。还是失败了 (6认同)
  • 对于那些将 git 存储库连接到您的应用程序的人:不要忘记提交文件。从 [docs](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-deploy.html) 开始,“如果安装了 git,EB CLI 将使用 git archive 命令创建 .zip 文件来自最近 git commit 命令的内容。” - 如果您不提交,该文件将不会存在于您的下一个“eb 部署”中 (5认同)
  • 我也是。尝试使用Node 8.4.0,npm 5.3.0安装Puppeteer时发生相同的错误。谢谢! (2认同)
  • 好的解决方案 这击败了各种脆弱的.ebextensions基于变通办法的工作,请放下! (2认同)

Mar*_*arz 5

通过覆盖初始化服务的脚本中的一些配置,我和我的团队能够在Amazon NodeJS机器上实现此功能。这实际上使用完全相同的脚本和几个额外的命令覆盖了包含的aws节点运行配置。这是您要放置的文件.ebextensions

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash
      #==============================================================================
      # Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
      #
      # Licensed under the Amazon Software License (the "License"). You may not use
      # this file except in compliance with the License. A copy of the License is
      # located at
      #
      #       http://aws.amazon.com/asl/
      #
      # or in the "license" file accompanying this file. This file is distributed on
      # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
      # implied. See the License for the specific language governing permissions
      # and limitations under the License.
      #==============================================================================

      chmod 777 -R /tmp
      set -xe

      sudo /opt/elasticbeanstalk/containerfiles/ebnode.py --action npm-install
Run Code Online (Sandbox Code Playgroud)