📅  最后修改于: 2023-12-03 15:15:32.425000             🧑  作者: Mango
As a programmer, managing multiple versions of Node.js on your machine can be quite challenging. Homebrew is a package manager for macOS that simplifies the installation of various software including Node.js. NVM (Node Version Manager) is a command-line tool that allows you to install and switch between different versions of Node.js effortlessly. This guide will walk you through the steps to install NVM using Homebrew and provide code snippets in markdown format.
Before we begin, make sure you have Homebrew installed on your macOS. If you don't have Homebrew installed, you can install it by executing the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Open a terminal and run the following command to install NVM using Homebrew:
brew install nvm
This command will install NVM (Node Version Manager) on your macOS.
After the installation, you need to configure your shell to use NVM. Run the following command to add the necessary configuration to your shell profile (e.g., ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
):
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.your_shell_profile
echo '. "/usr/local/opt/nvm/nvm.sh"' >> ~/.your_shell_profile
Replace ~/.your_shell_profile
with the path to your shell profile (e.g., ~/.bashrc
, ~/.bash_profile
, or ~/.zshrc
).
To start using NVM, reload your shell profile by running the following command:
source ~/.your_shell_profile
Now, you can install specific versions of Node.js using NVM. To install the latest stable version of Node.js, run the following command:
nvm install node
This will install the latest stable version of Node.js.
To install a specific version of Node.js, execute the following command:
nvm install <version>
Replace <version>
with the desired Node.js version (e.g., 14.17.0
, 12.22.1
, etc.).
After installing a version of Node.js, you can use it by running the following command:
nvm use <version>
Replace <version>
with the Node.js version you want to use.
To list all installed versions of Node.js on your machine, use the following command:
nvm ls
Congratulations! You have successfully installed NVM using Homebrew and learned how to manage multiple versions of Node.js on your macOS. Now you can easily switch between different Node.js versions based on your project requirements. Happy coding!