Introduction
Node.js is a strong JavaScript runtime that builders use to construct scalable purposes. Nonetheless, with new variations being launched incessantly, managing these variations could be tough. That is the place Node Model Supervisor (NVM) is available in. NVM is a command-line instrument that means that you can set up, replace, and swap between completely different Node.js variations simply.
Personally, I work on loads of completely different tasks, every of which had been both began at completely different instances or require completely different Node options, so having the ability to swap between Node variations simply is necessary.
On this Byte, we’ll concentrate on learn how to handle the default Node.js model with NVM.
Setting Default Node.js Model with NVM
Setting a default Node.js model with NVM is a simple course of. First, it is advisable be certain that NVM is put in in your system. If it isn’t, you’ll be able to obtain and set up it from the official NVM GitHub repository.
As soon as NVM is put in, you’ll be able to set up a selected Node.js model through the use of the nvm set up
command adopted by the model quantity. For instance, to put in Node.js model 14.15.1, you’ll run:
$ nvm set up 14.15.1
You may as well be extra generic and simply specify 14, for instance, which might then set up the newest model with the foremost model of 14.
After the set up, you’ll be able to set this model because the default utilizing the nvm alias default
command:
$ nvm alias default 14.15.1
Now, each new terminal session will use Node.js model 14.15.1 by default.
Switching to Newest Node.js Model as Default with NVM
If you wish to use the newest obtainable model of Node.js as your default, you are able to do so simply with NVM. First, it is advisable discover out the newest model. You are able to do this with the nvm ls-remote
command, which lists all of the Node.js variations obtainable for obtain:
$ nvm ls-remote
As soon as you’ve got recognized the newest model, you’ll be able to set up it and set it because the default, similar to we did within the earlier part:
$ nvm set up <latest-version>
$ nvm alias default <latest-version>
Change <latest-version>
with the precise model quantity.
Switching to LTS Node.js Model as Default with NVM
Lengthy-Time period Help (LTS) variations of Node.js are variations which are supported and maintained by the Node.js staff for an extended interval. These variations are typically extra secure and beneficial for manufacturing environments.
To change to the newest LTS model, you should use the nvm set up --lts
command, which is able to set up the newest LTS model:
$ nvm set up --lts
After which set it because the default:
$ nvm alias default --lts
Notice: The --lts
flag will also be used with the nvm alias default
command to set the newest LTS model because the default.
This manner, you’ll be able to be certain that your improvement surroundings is at all times utilizing a secure, long-term supported model of Node.js.
Checking Node.js Model in VS Code
In VS Code, you’ll be able to simply test the model of Node.js that is at present in use. To do that, you will have to open the terminal inside VS Code. For those who’re unsure learn how to open the terminal, you are able to do it by clicking on View
within the menu, after which choosing Terminal
.
As soon as the terminal is open, you’ll be able to test the Node.js model by getting into the next command:
$ node -v
This command prompts Node.js to return the model quantity. The output ought to look one thing like this:
v14.15.1
The v
stands for model, and the numbers following it symbolize the model of Node.js that’s at present lively. On this case, the model is 14.15.1.
Notice: Keep in mind, the model of Node.js that VS Code makes use of is set by the system PATH, or by the node
model set by NVM for those who’re utilizing it. For those who’ve put in a number of variations of Node.js, you’ll be able to swap between them utilizing NVM.
If for some cause you are still seeing an older model when working the above instructions, attempt restarting VS Code.
Conclusion
Managing completely different variations of Node.js generally is a bit tough, particularly whenever you’re engaged on a number of tasks that every require a special model. Nonetheless, with the Node Model Supervisor (NVM), you’ll be able to simply swap between completely different variations of Node.js as wanted. Not solely does this make it simpler to handle your tasks, but additionally helps just remember to’re at all times utilizing the proper model of Node.js.