Code build is failed with Error while executing command: npm install. Reason: exit status 127
Asked Answered
A

5

15

I created a code pipeline with very simple code and connected to codecommit. tried to build it but it is failing at codebuild step stating error executing npm install. Am I missing something? Sorry I was new to this codebuild/ codepipeline.

Below is the log for codebuild failure:

[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: 

My buildspec.yml file looks like this:

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
Akela answered 15/2, 2019 at 13:3 Comment(0)
B
11

The error message is a few lines down your logs : sh: 1: npm: not found.

This means the npm command is not available in the build environment. Did you correctly select a nodejs build ?

When you create a build environment, you have to specify the operating system and the runtime. It is very unlikely here that you did not specifcy nodejs as runtime.

See step by step guide here : https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started.html#getting-started-create-build-project

See documentation here : https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html

And see a screenshot from my console here : enter image description here

Baguette answered 15/2, 2019 at 13:43 Comment(0)
T
2

Just posting this here in case someone else comes across it in the future. OP buildspec.yaml should work if updated to the following BuildSpec Reference

Changelog:

  • updated buildspec to version .2
  • added a nodejs dependency to the install phase
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
Takin answered 19/10, 2020 at 21:46 Comment(0)
P
1

I had the same issue. To see the actual issue please check the build logs carefully. It's not the error shown in red, but few line above this.

(1) Something to do with permission od the code build service role. I assigned the administration access temporarily and it worked but failed due to different reason. To check it, try to execute the simple cloud-formation command like ‘aws cloudformation list-stacks’ only ( try it with inline command option or leave this command only and see it works.

(2) The issue with mine was a samtemplate file (I was trying with C#), in your case serverless.yml. It was creating a build file in one location, but my samtemplate referring to a different location. if possible, try the same command from you AWS CLI in you local machine

There may be a chance you have selected a wrong Os/image etc.. Check https://docs.aws.amazon.com/codebuild/latest/userguide/getting-started-create-build-project-console.html

Polyurethane answered 5/7, 2020 at 9:38 Comment(0)
V
1

Adding as it may help someone. I received an error that read:

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

It turned out this was due to the quotation marks.

Velours answered 6/10, 2021 at 10:14 Comment(0)
T
0

In my case, I had the following buildspec.yml:

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 18
    commands:
      - npm install
  build:
    commands:
      - npm run build
artifacts:
  files:
    - 'dist/**/*'
    - 'node_modules**/*'
    - 'package.json'
    - 'package-lock.json'

The Amazon Linux image do not have support for node 18 (as of October 2023). Therefore I had to change the image to the Linux Standard 7 (you can check each image here. Inside each there is a runtimes.yml file that states which versions of each language are available in that image.

To change the images you have to go to CodeBuild -> Edit -> Environment

Tooley answered 26/10, 2023 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.