If, like me, you regularly switch between different projects using different versions of nodeJS and NPM, this article is going to save you precious time!

Help, I need to use two versions of nodeJS at the same time!

We all know this problem: on different projects we need a specific version of nodejs and npm that's never the same one.

And often we find out in a none too explicit way when our WebPack script sneakily crashes with an error message that's, to say the least, incomprehensible:

Example of an error you get when running our script with the wrong version of nodeJS

The error message even rubs it in by specifying: This is probably not a problem with npm. In other words "It doesn't work and it's not my fault, kid!".

The naive solution would be to regularly update your working environment and always keep up-to-date versions of your tools. However, plenty of reasons can explain why you keep using older versions of nodeJS, especially with a somewhat older project and/or one where you're collaborating with other teams.

Choosing your Node version: NVM to the rescue

NVM is a command-line tool that will let you easily switch from one version of nodeJS to another.

Installing NVM

To install it, you just need to run this command:

Once that's done, I recommend restarting your terminal, and checking that everything's set up correctly by typing nvm -v. If you get anything other than an error, the install went well.

Congrats, you can now use NVM to change your node version by simply typing nvm use <version>. For example nvm use 14 or nvm use 14.7.

You'll then notice (by typing npm -v) that your nodeJS version has changed. You'll be able to move forward on your legacy project again!

Now careful, the nodeJS version has changed only for the current terminal session, and that's the whole trick! That's exactly what lets you use two versions of nodeJS in two tabs of your Terminal. So you'll need to run nvm use again every time you start a new instance of your terminal.

Installing the versions of NodeJS you need

Sometimes, the nvm version isn't yet present on your system and you'll get an error like this: N/A: version "17 -> N/A" is not yet installed. You need to run "nvm install 17" to install it before using it.,.

If that's the case, all you need to do is install the missing version by typing nvm install <version>.

Going back to the system's nodeJS version

If for whatever reason you want to go back to the default version of nodeJS, all you need to do is type nvm use default or nvm use system depending on your operating system.

Remembering my nodeJS version for each project

It's not necessarily easy, or even desirable, to remember off the top of your head which nodeJS version to use for each project.

The trick here is to add a small file named .nvmrc in your project's directory, which will contain the nodeJS version number that NVM should use.

To find the exact version of nodeJS currently in use, all you need to do is type nvm current.

Thanks to this file, NVM will now know which nodeJS version to use, and we'll just need to type nvm use in our directory, without having to specify a version.

Creating the .nvmrc file

To make creating the file easier, we can script the creation of our .nvmrc file using a function we'll call nvmdump:

We can now create our .nvmrc file very quickly and without risk of error by typing the nvmdump command in our terminal.

Automatically switching node versions

We've seen how to specify at the directory level the nodeJS version NVM should use using a .nvmrc file. But now we'd like to automatically switch to the right version, without having to run nvm use every time we change folders.

This question was raised in this StackOverflow thread. The most elegant solution offered suggests using this ZSH script, to place directly in your .zshrc file.

When you navigate into a folder using the cd command, it will systematically check for the presence of a .nvmrc file and run nvm use if needed.

For BASH users, you'll probably just need to tweak the script a little. That said, I can only strongly encourage you to replace BASH with ZSH.

Fixing the "Default -> N/A" is not yet installed error

The first time I ran this script on macOS Monterey, I got a weird message:

N/A: version "default -> N/A" is not yet installed. You need to run "nvm install default" to install it before using it.

To fix this problem I simply ran this command to create an alias for the default environment:

Conclusion

So it's not very complicated to change your nodeJS version as easily as changing your shirt, you just need the right tool and NVM does the job wonderfully.

I hope this tip is useful to you and saves you precious time. Feel free to share this article with anyone it might help, it would make me really happy and encourage me to offer more tips like this one in the future!

Sources  :