【问题标题】:Beanstalk:Node.js 部署 - node-gyp 由于权限被拒绝而失败
【发布时间】:2018-02-10 14:15:39
【问题描述】:

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

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

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

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

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

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

该应用程序是通过上传一个不包含node_modules 的 .zip 文件手动部署的。 IE。它不是通过eb 命令行工具部署的。

【问题讨论】:

    标签: node.js amazon-elastic-beanstalk


    【解决方案1】:

    解决方案

    解决方法是将文件.npmrc添加到应用程序中,内容如下:

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

    (或以任何其他方式配置 npm。(尽管在 /opt/elasticbeanstalk/env.vars 中设置 npm_config_unsafe_perm=true 对我不起作用。)

    说明

    npm install 由 root 用户运行,但它为某些包触发的 node-gyp 进程由默认用户 ec2-user 运行。此用户无法访问由 npm install 运行创建并由 root 拥有的 /tmp/deployment/application/node_modules/ 目录。 (而且它可能也无法访问由它们创建的/tmp/.npm/tmp/.config。)通过启用unsafe-perm,我们强制npm 也以root 身份运行node-gyp,从而避免了这个问题。

    (我个人更愿意以ec2-user 而不是root 运行所有代码,但我想这会更复杂:-))

    学分

    unreal0 has pointed me to the solution

    相关问题

    【讨论】:

    • 谢谢! unsafe-perm=true in .npmrc 为我解决了这个问题。
    • 我也是。尝试使用 Node 8.4.0、npm 5.3.0 安装 Puppeteer 时出现同样的错误。谢谢!
    • 很好的解决方案。这击败了各种脆弱的基于 .ebextensions 的解决方法,放下手!
    • 对于那些将 git repo 连接到您的应用程序的人:不要忘记提交文件。来自docs“如果安装了 git,EB CLI 使用 git archive 命令从最近的 git commit 命令的内容创建一个 .zip 文件。” - 如果您不提交,该文件将不会存在于您的下一个“eb 部署”中
    • 这个解决方案对我不起作用。它仍然失败
    【解决方案2】:

    我和我的团队通过覆盖初始化服务的脚本中的一些配置,能够在 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
    

    【讨论】:

    • 我尝试了其他配置文件,但这个有效!谢谢!
    【解决方案3】:

    我修复了实例上的 aws 配置。 t2.micro => t2.small 或更大的一个。 enter link description here

    【讨论】:

    • 这是链接唯一的答案。
    • 这是正确的。更改实例大小将解决此问题。
    【解决方案4】:

    就我而言, 通过在“~/.npmrc”中设置“unsafe-perm=true”解决

    【讨论】:

      【解决方案5】:

      我们需要在这里做两件事。

      第一个:如果您的项目根目录中还没有 .ebextensions 文件夹,请创建它。然后在 .ebextensions 中创建一个名为 01_fix_permissions.config 的文件。

      然后再启用 PROXY set -xe/opt/elasticbeanstalk/bin/healthd-track-pidfile --proxy nginx

      files:
      "/opt/elasticbeanstalk/hooks/appdeploy/pre/49_change_permissions.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/usr/bin/env bash
        sudo chown -R ec2-user:ec2-user tmp/
        set -xe
        /opt/elasticbeanstalk/bin/healthd-track-pidfile --proxy nginx
      

      【讨论】:

      • 代理的东西有什么作用?
      • 我认为代理的东西很可能不是手头的问题所特有的,可以忽略。
      【解决方案6】:

      我需要创建并提交一个.npmrc 文件和一个.ebextensions/01-permissions.config 文件来解决这个问题:

      www$ cat .npmrc
      # Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5
      unsafe-perm=true
      www$ cat .ebextensions/01-permissions.config 
      files:
        "/opt/elasticbeanstalk/hooks/appdeploy/pre/49_change_permissions.sh":
          mode: "000755"
          owner: root
          group: root
          content: |
            #!/usr/bin/env bash
            sudo chown -R ec2-user:ec2-user tmp/
      www$ 
      
      

      【讨论】:

        猜你喜欢
        • 2021-05-18
        • 2013-07-31
        • 2021-07-27
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        • 2020-01-04
        • 1970-01-01
        相关资源
        最近更新 更多