Error while building simple API with AWS CodePipeline, CodeBuild

1.3k Views Asked by At

I have been attempting to set up auto-deploy from my github repo for this simple REST API and cannot get it to stop erroring on 'npm install'. It is your basic node project with express. Here is my buildspec.yml:

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - echo Installing
  pre_build:
    commands:
      - echo Installing source NPM dependencies.
      - npm install
  build:
    commands:
      - echo Build started on `date`
      - echo Compiling the Node.js code
      - npm run build
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - '**/*'

Here is the error prinout from AWS CodeBuild:

[Container] 2022/05/15 21:13:45 Running command npm install
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

npm ERR! code EEXIST
npm ERR! path /codebuild/output/src331276253/src/node_modules/.bin/nodemon
npm ERR! Refusing to delete /codebuild/output/src331276253/src/node_modules/.bin/nodemon: is outside /codebuild/output/src331276253/src/node_modules/nodemon and not a link
npm ERR! File exists: /codebuild/output/src331276253/src/node_modules/.bin/nodemon
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-05-15T21_13_48_178Z-debug.log

[Container] 2022/05/15 21:13:48 Command did not exit successfully npm install exit status 1
[Container] 2022/05/15 21:13:48 Phase complete: PRE_BUILD State: FAILED
[Container] 2022/05/15 21:13:48 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm install. Reason: exit status 1

I think I've done this 100% as convention dictates, except for maybe my project structure. Rather than having all my files in 'src', they are in the root of the project.

1

There are 1 best solutions below

0
On

Maybe you found the answer already, but for future readers, here it goes:

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!

According to this message, the reason why the install fails is because you used different versions of npm on local machine vs on codebuild.

Your codebuild is using nodejs: 10 runtime, so according to the screenshot below (taken from https://nodejs.org/en/download/releases/), it must be using npm v6.8.x that comes with that version of node.

Node/NPM versions matrix

That version of npm generates/reads package-lock.json file with a version 1 format, while your local npm has generated a package-lock.json with the version 2 format, most certainly because you are on npm v7 or later.

Solution:

Update npm version in the pre-build phase of your Codebuild, as such:

  pre_build:
    commands:
      - echo Installing source NPM dependencies.
      - npm install -g npm@<your_local_version_here>
      - npm ci

Also, it better to use npm ci rather than npm install on CI, because the former leverage the existing package-lock.json to ensure reproductible builds.