How to Quickly Change Node Versions Between Projects Using Nvm And Shell Scripting

I switch between projects that use different versions of node very often. What I typically have to do is open the package.json and find the node version then use nvm to switch to that node version. I do this often enough that I wanted to automate this process to save some time.

Here is my solution:

I have a function to find the node version and a function to switch to that version.

First, in the what_node function I use ripgrep (rg) to find the line in the package.json file that holds the node version. Note I’m only looking in the current directory as some sub directories could have their own package.json files.

Make sure line numbers and the filenames are turned off in the output to get clean results.

> rg \"node\" 'package.json' --no-line-number --no-filename
    "node": "10.15.0"

Then I awk this over using the colon as the delimiter.

> rg \"node\" 'package.json' --no-line-number --no-filename | awk -F ':' '{print $2}'

Now that we have our node version I clean this up with translate (tr) to remove the quite and any white space.

> rg \"node\" 'package.json' --no-line-number --no-filename | awk -F ':' '{print $2}' | tr -dc '0-9.\n'

Next is to create a function that will take our node version and switch us over to it using nvm. Because nvm might not have the exact version of node I need for this project I awk the node version to get the major version number and save this in a variable.

> what_node | awk -F '.' '{print $1}'

Finally, I use this variable as a check to see if it found anything and with use nvm to switch to that version or output a message telling me no node version was found.

Put together this is what you get:

what_node() {
    rg \"node\" 'package.json' --no-line-number --no-filename | awk -F ':' '{print $2}' | tr -dc '0-9.\n'

switch_node() {
    local NODE_VERSION=$(what_node | awk -F '.' '{print $1}')
    if [ -z "${NODE_VERSION}" ]; then
        echo "\nNo node version found.\n"
        echo "Project wants node: \e[32m$(what_node)\e[m"
        printf "\e[33m"
        nvm use "v$NODE_VERSION"
        printf "\e[m"


> switch_node
Project wants node: 10.15.0
Now using node v10.18.1 (npm v6.13.4)

You would place these shell script functions in your .bash_profile or .zshrc files depending on what shell you are using.