Environment
- OS: macOS Catalina (10.15)
- Node: 12.10.0
Installing the Target Node Version
# Check the node versions installed via nodenv
# The system's built-in node and 10.16.3 are currently installed
$ nodenv versions
system
* 10.16.3 (set by ~/.nodenv/version)
It looks like the version we want to install isn't installed in nodenv yet, so let's install it with nodenv install 12.10.0. You can also check the list of installable versions with nodenv install --list.
# Check the list of installable versions
$ nodenv install --list
# Install 12.10.0 via nodenv
$ nodenv install 12.10.0
Downloading node-v12.10.0-darwin-x64.tar.gz...
-> https://nodejs.org/dist/v12.10.0/node-v12.10.0-darwin-x64.tar.gz
Installing node-v12.10.0-darwin-x64...
Installed node-v12.10.0-darwin-x64 to ~/.nodenv/versions/12.10.0
# Check with the nodenv versions command that it was installed
# If 12.10.0 shows up, the install succeeded
$ nodenv versions
system
* 10.16.3
12.10.0
Setting Up a Local Environment
Now let's set up per-directory environments. Just move to the target directory and run nodenv local 12.10.0.
# Move to the target directory and specify the version
$ mkdir LocalNodeTest
$ cd LocalNodeTest
$ nodenv local 12.10.0
## Check the version
$ node -v
v12.10.0
$ nodenv versions
system
10.16.3
* 12.10.0 (set by {target directory}/LocalNodeTest/.node-version)
Now, only within LocalNodeTest is the Node version set to 12.10.0. That was really easy to do. The version information is written to LocalNodeTest/.node-version, and it's a good idea to keep this file under git management so the version used by the project is documented clearly.