nvm (Node Version Manager) is a common version manager for Node.
π Installation β
- Install Script: Run the installation script from the NVM repository:
bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash- Reload Shell: Close and reopen your terminal and run
bash
source ~/.bashrczsh
source ~/.zshrczsh
omz reloadNote
Do not use source ~/.zshrc when using oh-my-zsh. More details
- Verify Installation: Check if NVM is installed correctly
bash
nvm --versionπ Basic Commands β
- Install a Node Version: To install a specific version of Node.js:
bash
nvm install 14.17.0- Use a Node Version: To switch to a particular Node.js version:
bash
nvm use 14.17.0- List Installed Versions: To see all Node.js versions installed on your machine:
bash
nvm ls- List Available Versions: To see all available Node.js versions:
bash
nvm ls-remote- Set Default Node Version: To set a default Node.js version:
bash
nvm alias defualt 20.17.0π§ββοΈ Advanced Tips and Tricks β
- Upgrade NVM: Keep NVM up to date to enjoy the latest features and bug fixes:
bash
nvm install-latest-npm- Uninstall a Node Version: Remove a Node.js version you no longer need:
bash
nvm uninstall 14.17.0- Run Specific Node Version Temporarily: Run a specific Node.js version for a single command:
bash
nvm run 14.17.0 my-script.js- Global Packages: When switching Node.js versions, your global packages might differ. Use
nvmto manage global packages for each version.
π Setting Up .nvmrc for Project-Specific Node Versions β
When working on multiple projects, itβs often helpful to specify which Node.js version each project uses. This is where the .nvmrc file comes in handy.
- Create a
.nvmrcFile: In the root directory of your project, create a file named.nvmrc:
Specific
echo "14.17.0" > .nvmrcSystem
node -v > .nvmrc- Use the
.nvmrcFile: To automatically use the Node.js version specified in.nvmrc, navigate to your project directory and run:
bash
nvm useThis command reads the .nvmrc file and switches to the specified version.
- Set Up Automatic Version Switching: For a smoother workflow, you can use a tool like
nvm-autoornvm-useto automatically switch Node.js versions when you navigate into your project directory. Installnvm-autoglobally:
bash
npm install -g nvm-autoNow, nvm-auto will automatically switch to the Node.js version specified in your .nvmrc file when you enter your project directory.
