Are you currently working on a Node.js project using AWS CodeBuild and encountering challenges in seamlessly integrating NVM commands? In this blog post, I’ll walk you through the process of smoothly incorporating NVM into your AWS CodeBuild setup. No need to reopen terminals or reload bash profiles – I’ve got a custom solution for you.

If you’ve been scouring the internet for solutions without success, fret not. I faced the same issue and devised a unique solution that worked wonders for me. Now, let’s delve into the specifics of my AWS CodeBuild project.

AWS CodeBuild Project Details:

Here’s a quick rundown of the essential details:

  • Image: Linux Standard 2.0.0
  • AWS Service: CodeBuild
  • File Name: buildspec.yaml/ buildspec.yml
  • Repository: Node Project

Assuming you already have a pipeline set up to trigger your build project on repository changes, if not, check out this Blog for a detailed guide on creating CI/CD in AWS. Now, let’s proceed with the necessary files in your repository’s root folder. You can also download the source code from GitHub.

Buildspec.yaml:

version: 0.2 
phases: 
  install:
    runtime-versions:
        nodejs: 10
    commands: 
    - apt-get update 
    - apt-get install build-essential libssl-dev
    - pip install awscli==1.16.141
    - curr_dir=$(pwd)
  build: 
    commands:
    - cd $HOME
    - ls -lrta
    - node --version
    - npm --version
    - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
    - bash $CODEBUILD_SRC_DIR/codebuild_nvm_wrapper.sh

codebuild_nvm_wrapper.sh:

- NVM_DIR="$HOME/.nvm"
- mkdir -p $NVM_DIR
- cd $(mktemp -d) 
- . $NVM_DIR/nvm.sh 
- cd $CODEBUILD_SRC_DIR
- nvm install 8.10 
- nvm use v8.10

Make sure you create both files and use Buildspec.yaml as your source build file. With these files in place, you can effortlessly leverage NVM commands during your build project without the hassle of reopening terminals or reloading bash profiles.

Elevate your AWS CodeBuild experience for Node.js projects – dive into seamless NVM integration today!