mac uses nvm to manage node and common usage and precautions

Original link: https://wlnxing.com/archives/71.html

foreword

I have been developing on windows before and using nvm to manage the node version. You can use a node version in one window and you can also alias the node version. It is very convenient to switch directly with the alias.

Recently, I also need to develop on the mac, but the method of using nvm on the mac is a bit different from the method used on the windows.

before installation

My mac has been upgraded to a new version of macOS Monterey, version 12.4

It is recommended to be the same as my environment, at least the major version is the same as mine, and then install it freshly, at least the system itself does not have a node environment anymore. Uninstall node cleanly

Install

install git

nvm needs a git environment, please make sure you have git locally

Verification method: Enter in the terminal: git

If so, it means that you already have git, you can skip directly to the next step

If there is this prompt, it means that git is not installed locally


Follow the prompts to install

If the git installation network that comes with this system is slow, you can download the xcode command line installation package from me

You can also go to the official website to download the latest xcode

install nvm

Open the terminal and enter the following command to create a new zsh environment variable file used by mac

 cd ~ touch .zshrc

Then go directly to nvm’s github address: https://github.com/nvm-sh/nvm#installing-and-updating
Find the installation command and copy the installation command to the terminal for execution

You can also copy the following commands directly (note the version)

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

There is interference in the domestic network, please set up a proxy in the terminal, you know


If there is no local ladder to access the github address or script address, I will provide a reverse proxy, and use it in a low-key manner and prohibit publicity!

Instructions:

Replace raw.githubusercontent.com in the url in the command with magicrawgithubusercontent.wlnxing.com example, the installation command provided above is replaced with:

 curl -o- https://magicrawgithubusercontent.wlnxing.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Any other script that needs a proxy can be replaced like this
Note: This reverse generation will replace magicrawgithubusercontent.wlnxing.com raw.githubusercontent.com there may be some unexpected problems due to brute force replacement.


Here is a screenshot of the installation being completed:

After the installation is complete, enter nvm directly to verify the installation

If you open a new window and use nvm to prompt that the command is not found, there is a high probability that there is a problem with the environment variable settings. It is recommended to manually paste the following command in the .zshrc file:

 export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

After saving, command + Q to completely exit the terminal or restart the computer directly and then open the terminal

change source (optional)

The domestic environment node download is very slow, it is recommended to replace the node download address with Taobao source

Enter directly on the command line:

 echo export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node >> .zshrc

Just

How to use and some tips

Install multiple versions

  • List all node versions: nvm ls-remote node
  • Download (install) the version node: nvm install 版本号
    Such as: nvm install v12.10.0

Note: If node is not currently installed after nvm install xxx, the current default node version will be automatically set to the installed node version

  • List downloaded (installed) versions: nvm ls

switch between different versions

  • Give the node version an alias: nvm alias 别名
    Such as: nvm alias vue3
  • Temporarily use a version in a window: nvm use 版本号/别名
    Such as: nvm use v12.10.0 or nvm use vue3
  • Set the default node version: nvm alias default 版本号/别名
    Such as: nvm alias default v16.16.0 or alias method: nvm alias default vue3

Note:
nvm use xxxx command will only switch versions in the current command line window. If you reopen the window, you need to re- nvm use xxx once.
If the project needs to fix a ndoe version and doesn’t want to remember it, you can create a .nvmrc file in the project directory to specify the ndoe version used in this folder, so that you don’t need to bring the version number when you use it.
With this combination of punches, I can use multiple node versions in multiple windows at the same time, which is not too cool~

Personally, it is recommended to use aliases for versions, so that when switching versions, you can directly switch according to the aliases without memorizing the version number.

  • When using a lower version of node, if this error is reported: bad CPU type in executable: node is:closed This is because the lower version node does not have an arm version, but it can be solved by installing an escape tool: command line input: softwareupdate --install-rosetta After installation, restart the terminal

This article is reprinted from: https://wlnxing.com/archives/71.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment