【问题标题】:Code build is failed with Error while executing command: npm install. Reason: exit status 127执行命令时代码构建失败并出现错误:npm install。原因:退出状态 127
【发布时间】:2019-07-09 15:14:20
【问题描述】:

我用非常简单的代码创建了一个代码管道并连接到 codecommit。尝试构建它,但在代码构建步骤中失败,说明执行 npm 安装时出错。我错过了什么吗?抱歉,我是这个 codebuild/codepipeline 的新手。

以下是代码构建失败的日志:

[Container] 2019/02/15 11:47:39 Waiting for agent ping 
[Container] 2019/02/15 11:47:40 Waiting for DOWNLOAD_SOURCE 
[Container] 2019/02/15 11:47:40 Phase is DOWNLOAD_SOURCE 
[Container] 2019/02/15 11:47:40 CODEBUILD_SRC_DIR=/codebuild/output/src501317273/src 
[Container] 2019/02/15 11:47:40 YAML location is /codebuild/output/src501317273/src/buildspec.yml 
[Container] 2019/02/15 11:47:40 Processing environment variables 
[Container] 2019/02/15 11:47:40 Moving to directory /codebuild/output/src501317273/src 
[Container] 2019/02/15 11:47:40 Registering with agent 
[Container] 2019/02/15 11:47:40 Phases found in YAML: 1 
[Container] 2019/02/15 11:47:40 BUILD: 2 commands 
[Container] 2019/02/15 11:47:40 Phase complete: DOWNLOAD_SOURCE Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:40 Entering phase INSTALL 
[Container] 2019/02/15 11:47:40 Phase complete: INSTALL Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:40 Entering phase PRE_BUILD 
[Container] 2019/02/15 11:47:40 Phase complete: PRE_BUILD Success: true 
[Container] 2019/02/15 11:47:40 Phase context status code: Message: 
[Container] 2019/02/15 11:47:41 Entering phase BUILD 
[Container] 2019/02/15 11:47:41 Running command npm install 
sh: 1: npm: not found 

[Container] 2019/02/15 11:47:41 Command did not exit successfully npm install exit status 127 
[Container] 2019/02/15 11:47:41 Phase complete: BUILD Success: false 
[Container] 2019/02/15 11:47:41 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm install. Reason: exit status 127 
[Container] 2019/02/15 11:47:41 Entering phase POST_BUILD 
[Container] 2019/02/15 11:47:41 Phase complete: POST_BUILD Success: true 
[Container] 2019/02/15 11:47:41 Phase context status code: Message: 
[Container] 2019/02/15 11:47:41 Expanding base directory path: . 
[Container] 2019/02/15 11:47:41 Assembling file list 
[Container] 2019/02/15 11:47:41 Expanding . 
[Container] 2019/02/15 11:47:41 Expanding artifact file paths for base directory . 
[Container] 2019/02/15 11:47:41 Assembling file list 
[Container] 2019/02/15 11:47:41 Expanding post-saml.yaml 
[Container] 2019/02/15 11:47:41 Skipping invalid artifact path post-saml.yaml 
[Container] 2019/02/15 11:47:41 Expanding beta.json 
[Container] 2019/02/15 11:47:41 Found 1 file(s) 
[Container] 2019/02/15 11:47:41 Phase complete: UPLOAD_ARTIFACTS Success: true 
[Container] 2019/02/15 11:47:41 Phase context status code: Message: 

我的 buildspec.yml 文件如下所示:

version: 0.0
environment_variables:
    plaintext:
        "INPUT_FILE": "serverless.yml"
        "S3_BUCKET": ""
containers:
    LambdaFunctions:
        phases:
            during_build:
                commands:
                    - npm install
                    - aws cloudformation package --template $INPUT_FILE --s3-bucket $S3_BUCKET --output-template post-saml.yaml
        artifacts:
            files:
                - post-saml.yaml
                - beta.json

【问题讨论】:

    标签: amazon-web-services npm aws-codebuild


    【解决方案1】:

    错误消息是您日志中的几行:sh: 1: npm: not found

    这意味着 npm 命令在构建环境中不可用。您是否正确选择了 nodejs 构建?

    创建构建环境时,必须指定操作系统和运行时。您不太可能没有将 nodejs 指定为运行时。

    在此处查看分步指南:https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started.html#getting-started-create-build-project

    在此处查看文档:https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html

    并在此处查看我的控制台的屏幕截图:

    【讨论】:

      【解决方案2】:

      只是在这里发布,以防其他人将来遇到它。如果更新为以下内容,OP buildspec.yaml 应该可以工作 BuildSpec Reference

      更新日志:

      • 将构建规范更新到版本 .2
      • 在安装阶段添加了 nodejs 依赖项
      version: 0.2
      env:
        variables:
          INPUT_FILE: "serverless.yml"
          S3_BUCKET: ""
      phases:
        install:
          runtime-versions:
            nodejs: 10
          commands:
            - npm install
        build:
          commands:
            - aws cloudformation package --template $INPUT_FILE --s3-bucket $S3_BUCKET --output-template post-saml.yaml
      artifacts:
          files:
              - post-saml.yaml
              - beta.json
      

      【讨论】:

        【解决方案3】:

        我有同样的问题。要查看实际问题,请仔细检查构建日志。不是红色显示的错误,而是上面几行。

        (1) 与代码构建服务角色的权限有关。 我临时分配了管理访问权限,它工作但由于不同的原因而失败。 要检查它,请尝试仅执行简单的 cloud-formation 命令,如“aws cloudformation list-stacks”(尝试使用内联命令选项或仅保留此命令并查看它是否有效。

        (2) 我的问题是一个 samtemplate 文件(我正在尝试使用 C#),在您的情况下是 serverless.yml。它在一个位置创建一个构建文件,但我的 samtemplate 指的是不同的位置。如果可能,请在您的本地计算机上尝试使用 AWS CLI 执行相同的命令

        您可能选择了错误的操作系统/图像等。检查 https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started-create-build-project-console.html

        【讨论】:

          【解决方案4】:

          添加可能对某人有所帮助。我收到一条错误消息:

          /codebuild/output/tmp/script.sh: 4: ‘npm: not found
          

          原来这是由于引号引起的。

          【讨论】:

            猜你喜欢
            • 2020-11-27
            • 1970-01-01
            • 2019-05-02
            • 1970-01-01
            • 2020-11-16
            • 1970-01-01
            • 2019-05-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多