Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a .nvmrc file which automatically change node version

Hi I have two projects one in angularjs 4.4.7 and another in angular 6 version. I need to switch between node version for this. I tried using NVM which is working manually. How to handle the version change inside the angularjs program to change the node version when automatically the latest angular page gets loaded. Is there a possible way like that. I went through the #avn also but how to create the .node-version file. Can someone help with any link or correct sample steps

like image 800
Janani Hariharan Avatar asked Jul 19 '19 10:07

Janani Hariharan


People also ask

How do I switch between node versions?

Switching among Node. 7; we can simply run either nvm use 12.22. 7 or nvm use 16.13. 0 to easily switch into either version we need. Note that since we only have one version that begins with 12, 14, or 16, we can switch versions with a simple nvm use 16 , nvm use 14 , or nvm use 12 command.

What is the use of .nvmrc file?

You can lock your application with a specific node. js version. This will help you to isolate the dependencies of of your node.

Can I use different node version for project?

Node Version Manager (NVM) is a great tool and easy to switch between multiple node versions while working on projects that required different NodeJs versions. It saves a lot of development time by just switching to the version of nodejs needed.


1 Answers

As @Aditya-M-P has already mentioned you can run the following command inside your projects root directory to generate the .nvmrc to set a desired NodeJS version for you project to work properly:

node -v > .nvmrc 

It will generate something like this inside your .nvmrc file:

v10.16.2 

Also using 10.16.2 without the v letter will work just fine.

However, in the official documentation in the .nvmrc section it never mentions that once you get this file created, the specified node version will be loaded automatically. So that's not enough, you need to run the command below so that nvm can look for the .nvmrc file to load the specified version:

nvm use 

Here it is a gif for demoing purpose: enter image description here

To autoload the specified node version:

You need to add something else to your shell configuration depending on what you use bash or zsh

To get the exact configuration for each of them, please follow the instructions in the corresponding shell config section.

In my case I'm using zsh so I do need to add this at the end of my .zshrc file and here is the image that confirms it works like a charm:

# 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 

enter image description here

I hope it could be useful for anyone else facing the same question! 😎

like image 189
alexventuraio Avatar answered Sep 23 '22 09:09

alexventuraio