How do you manage Node.js version on your machine? You might go with single version without any version control tool. However, if you work on multiple projects where diffrent versions are used, it is necessary to use one of the management tools.
This post recommends nvm
, or Node Version Manager. (github page)
Please note this is for Mac OS Big Sur.
Why nvm?
If you are a developer who works on multiple node projects each of which uses different node version, it is quite annoying to switch the version for each.
However, nvm
makes it easy with a configuration file .nvmrc
.
You just put the file under your project root folder. That's it.
The nvm
automatically switches the version to the specified in the .nvmrc
.
The .nvmrc
is like this.
14.17.5
OK, let's take advantage of it by taking steps below.
Uninsall Node.js
Uninstall nodebrew
If you already installed Node.js or one of the Node version manager tools like nodebrew
, please uninstall it.
$ brew uninstall nodebrew
Uninsall Node.js
Here is how to uninstall your Node.js on your machine.
$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom | while read f; do sudo rm /usr/local/${f}; done
$ sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
Insatall Node version
Install stable version
$ nvm install --lts
Installing latest LTS version.
v14.17.6 is already installed.
Now using node v14.17.6 (npm v6.14.15)
Install specific version
$ nvm install 14.17.5
Downloading and installing node v14.17.5...
Downloading https://nodejs.org/dist/v14.17.5/node-v14.17.5-darwin-x64.tar.xz...
############################################################################################################################################################################################### 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v14.17.5 (npm v6.14.14)
Set default version
You can specify the default version to use when no .nvmrc
is detected in the directory.
$ nvm alias default v14.17.5
default -> v14.17.5
❯ nvm ls
v12.14.1
-> v14.17.5
v14.17.6
system
default -> v14.17.5
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v14.17.6) (default)
stable -> 14.17 (-> v14.17.6) (default)
lts/* -> lts/fermium (-> v14.17.6)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.6 (-> N/A)
lts/fermium -> v14.17.6
.nvmrc
Locating .nvmrc
to your project root folter, you can employ the specified Node version for the project.
.nvmrc
14.17.5
In order to make .nvmrc
work, you need to add script codes in ~/.zshrc
.
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
Result
.nvmrc
12.14.1
When you go to the project root directory, automatically specified node version is applied!
Found '/somewhere/project_root/.nvmrc' with version <12.14.1>
Now using node v12.14.1 (npm v6.13.4)
$ node -v
v12.14.1
Cool.