Using nvm to switch Node.js versions on macOS

The CircleCI support document Switching Node.js Versions with NVM on macOS Executors gives the following example:

version: 2.1

jobs:
  build:
    macos:
      xcode: "14.2.0"  # Choose any supported Xcode image
    steps:
      - checkout

      - run:
          name: Use Node.js 20
          command: |
            nvm install 20
            nvm use 20
            node -v
            npm -v

      - run:
          name: Run tests
          command: |
            nvm use 20
            npm test

This implies that nvm use needs to be added to every step.


I’ve seen alternate solutions that add nvm alias to the step directly after nvm install and nvm use, which seems to make the nvm use command persistent to following steps. Is this a reliable solution that avoids repeated use of the nvm use command?

The example would then be as follows:

version: 2.1

jobs:
  build:
    macos:
      xcode: "14.2.0"  # Choose any supported Xcode image
    steps:
      - checkout

      - run:
          name: Use Node.js 20
          command: |
            nvm install 20
            nvm use 20
            nvm alias default 20
            node -v
            npm -v

      - run:
          name: Run tests
          command: |
            npm test