Keywords: Node.js | macOS | npm | Homebrew | nvm
Abstract: This article provides a detailed guide on installing Node.js, nvm, and npm on macOS using Homebrew. It covers installing nvm via Homebrew, managing Node.js versions with nvm, automatic installation and upgrading of npm, and installing additional tools like ionic and ngCordova. The article also discusses potential issues with Homebrew installation of nvm and offers manual installation as an alternative, ensuring readers can choose the most suitable method based on their needs.
Installing nvm
On macOS, using Homebrew to install nvm is a common approach. First, ensure Homebrew is updated to the latest version, then execute the installation command. After installation, it is necessary to add nvm's initialization script to the shell configuration file to load it automatically on each terminal startup. The specific steps are as follows:
brew update
brew install nvm
source $(brew --prefix nvm)/nvm.shTo make it permanent, add source $(brew --prefix nvm)/nvm.sh to ~/.profile, ~/.bashrc, or ~/.zshrc. For example, use the following command to add it to ~/.profile:
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profileIt is important to note that nvm's official documentation states that Homebrew installation is not supported. If issues arise, it is recommended to uninstall the Homebrew-installed nvm and use the manual installation method. Manual installation can be performed via a curl command that executes an installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashThis script clones the nvm repository to ~/.nvm and automatically adds the source line to the appropriate shell configuration files.
Installing Node.js
After installing nvm, it can be used to install and manage Node.js versions. nvm supports installing multiple Node.js versions and allows switching between them. For example, install Node.js version 0.10 or iojs version 1.2.0:
nvm install 0.10
# or
nvm install iojs-1.2.0It is recommended to install the latest LTS version to ensure stability and compatibility. For example, install Node.js version 16:
nvm install 16After installation, nvm automatically sets the installed version as the default, requiring no additional configuration.
Installing npm
npm (Node Package Manager) is typically bundled with Node.js, so it becomes available automatically after installing Node.js. However, to ensure the latest version of npm is used, an upgrade command can be executed:
npm install -g npm@latestNote that the previously recommended command npm update -g npm has been updated to the above command to ensure proper upgrading.
Installing Additional Tools
After Node.js and npm are installed, additional development tools can be installed. For example, install the ionic framework:
npm install -g ionicFor ngCordova, installation can be done using npm or bower based on project requirements. If using npm, navigate to the project directory and execute:
npm install --save ng-cordovaIf using bower, first install bower:
npm install -g bowerThen navigate to the project directory and install ngCordova:
bower install --save ngCordovaConsiderations
Some commands may require superuser privileges, which can be granted using the sudo prefix. Additionally, the npm install command can be abbreviated as npm i for efficiency. For example, npm install some_module can be shortened to npm i some_module.
In summary, installing nvm via Homebrew and managing Node.js versions with nvm is an efficient method, but attention should be paid to official support issues. Manual installation of nvm serves as an alternative to avoid potential compatibility problems. Regardless of the chosen method, ensure proper shell environment configuration and regularly update the toolchain to maintain an optimal development environment.