Skip to content

Instantly share code, notes, and snippets.

@cooperwfloyd
Last active July 29, 2023 19:24
Show Gist options
  • Save cooperwfloyd/51ce7748dba37d14dbabf5e942a2b4bb to your computer and use it in GitHub Desktop.
Save cooperwfloyd/51ce7748dba37d14dbabf5e942a2b4bb to your computer and use it in GitHub Desktop.
My nvm (Node Version Manager) Config for MacOS Ventura

My nvm (Node Version Manager) Config for MacOS Ventura

Install

Open up a new Terminal and run the following to install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Add Config

Create a .zshrc file in Macintosh HD -> Users -> {{your user folder}} if you don't have one already (or .bash_profile, .profile, or .bashrc if you're not using zsh) and add the following lines to load and automatically run nvm when opening a new Ternimal:

# Load nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# Add the lines below if you'd like nvm to be loaded automatically when you open a terminal
autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Check Installation

Run command -v nvm in your terminal. If nvm is returned then you have a successful installation.

Installing Node Versions

Use the nvm install command to install specific Node versions on your machine (ex. nvm install 14). nvm can install more specific versions as well (ex. nvm install 14.18.3).

To set a default node version for your machine, use nvm alias default followed by the version number (ex. nvm alias default 14).

Specifying Project-Specific Node Versions

If you don't already have one, create a .nvmrc file on the root of your project and simply type the Node version number that you'd like that project to use in the file and save. This should install any new Node versions automatically, but if it doesn't just run the nvm install command followed by the version number that you'd like to use.

Using the CLI

By adding the nvm loader to your terminal config, nvm should find and load the correct Node versions automatically, but if it doesn't or you don't want to use the automatic loader, you can use the nvm use command to load Node versions. If you have an .nvmrc file in your project, running nvm use will automatically use the version specified in the .nvmrc file, but you can also load specific versions by running nvm use followed by the version number (ex. nvm use 14).

Troubleshooting

Check the Troubleshooting on MacOS notes to ensure proper configuration. You might need to restart your Terminal windows or restart your Mac as well.

Master documentation is located on GitHub.

@coppinger
Copy link

This worked on macOS Monterey 12.6 on a MacBook Pro (13-inch, M1, 2020)

Several other guides weren't working, so thank you very much for taking the time to share this.

@cooperwfloyd
Copy link
Author

Glad to hear it @coppinger! Ran into a lot of situations where my co-workers couldn't get this working properly or were running into issues with the docs so I decided to document the process. Glad to hear that it is being helpful outside of our organization as well!

I see that you're on an M1 Mac — did you end up running into any issues in the installation process? Know some others have needed to install Rosetta or otherwise additionally configure their Apple Silicon machines in order to run the software.

Let me know, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment