node.jsaws-cdkaws-codebuildbuildspec

How to change nodejs file to 14 in buildspec for codebuild?


Here is my buildspec.yml file used by codebuild:

version: 0.2
env:
  shell: bash
phases:
  install:
    runtime-versions:
      nodejs: 14
   pre_build:
    commands:
      - echo Installing source NPM dependencies...
      - npm install -g aws-cdk
      - npm install -g typescript
      - npm install
      - npm run build 

  build:
    commands:
      - cdk --version 
      - cdk ls
      - cdk synth 
  post_build:

/usr/local/bin/cdk -> /usr/local/lib/node_modules/aws-cdk/bin/cdk npm WARN notsup Unsupported engine for aws-cdk@2.9.0: wanted: {"node":">= 14.15.0"} (current: {"node":"12.22.2","npm":"6.14.13"}) npm WARN notsup Not compatible with your version of node/npm: aws-cdk@2.9.0

Finally, cdk ls fails

Appreciate any help as I have already tried removing node-modules and package-lock.json.


Solution

  • In addition to setting the runtime-version in the buildspec, set the CodeBuild Project's environment prop to a build image type that supports Node 14. Currently, only Ubuntu Standard:5 does.

    new codebuild.Project(this, 'MyBuildProject', {
      environment: {
        buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
      },
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          install: {
            'runtime-versions': {
              nodejs: '14.x',
            },