a guide for properly installing nvm and node on macOS
nvm
A common method for installing Node.js is through nvm - Node Version Manager. This manager tool allows us to switch between different versions of Node. This is helpful if an update happens to break your code and you want to revert, or maybe you just have projects that use different Node versions.
Before beginning, you can check if you already have these tools using:
nvm -v
and for Node:
node -v which node # you can also use this which command to check where node is installed
My favorite way to install nvm is directly from the nvm github repository. Although some people use homebrew.
You can enter the following command into your terminal to begin the installation:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
You can check their guide below to download the latest version that's available: github.com - nvm - installing and updating After installing nvm, you may have to source nvm in your zsh profile (links the manager to your shell environment)
source nvm in your zsh profile
The ~/.zshrc
file is not complicated. It's just a text file that runs when you open the terminal. Navigate to this file (in the home directory), open it in a text editor and add the following lines:
export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
You may have to unhide hidden files to see it. You can scroll down to the helpful tips part of this post for a script that will at these lines of code for you.
The above code loads nvm and bash completion. To see the changes you will have to open a new terminal window or run source ~/.zshrc
(loads the commands from the .zshrc
profile into our current session). Confirm node version manager is installed by running nvm -v
and make sure you get the version printed into the console.
zachary@macintosh ~ % nvm -v 0.39.0
node
After installing nvm and linking it to your shell environment, you can use it to install node. Open a new terminal window and run:
nvm install --lts
--lts
specifies the latest version of node.
helpful tips
install yarn
npm install --global yarn
Here's a shell script to alter your ~/.zshrc
profile:
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.zshrc echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.zshrc source ~/.zshrc
helpful links
medium.com - sukiphan - how to install nvm Node.js
Published on: November 4, 2024
Edited on: November 27, 2024