In this article, We are going to show you best practise to install Node.JS in your MAC OS.
Here are the common problems you will face if you install Node.JS with installer or Single version
- Microservices problem
- AWS Lambda Function Problem
Microservices problem:
Let’s assume you have 2 different micro services
1. Microservice A using Node.JS version v6
2. Microservice B using Node.JS version v8
If you want to work on Microservice A you need to install Node.JS v6 on your system and for Microservice B Node.JS v8 then how you can switch from version v6 to v8. You will uninstall Node.JS v6 and Install Node.JS v8 and so on.
2. FAAS (Function as a Service) Problem
If you are familiar with FAAS most popular one is AWS lambda
Let’s assume you have 2 function A, B
1. Function A using Node.JS version v6
2. Function B using Node.JS version v8
If you want to update Function A you need to install Node.JS v6 on your system and for Function B Node.JS v8 then how you can switch from version v6 to v8. You will uninstall Node.JS v6 and Install Node.JS v8 and so on.
To solve this problem you need to install NVM (Node Version Manager)
NVM(Node Version Manager) Installation
Visit https://github.com/nvm-sh/nvm for more details
Install & Update script
To install or update nvm, you can use the install script using cURL:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
or Wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
after above command. You can see the .nvm directory is exist or not by typing following command
ls -a
as following screenshot
Then check your current path by following command
pwdYou can see following path display in the screenshot /Users/test
then check in your system ~/.bash_profile file is exist or not. Type following command it will list you the hidden files you can see in the screenshot below
ls -a
update ~/.bash_profile file using following command by typing in the terminal
sudo nano ~/.bash_profile
once the file is opened then add the following line
Note: “/Users/test/.nvm” test is a username you can replace with your username i.e “/Users/yourusername/.nvm”
export NVM_DIR="/Users/test/.nvm"
then add following lines below the above line
# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"# This loads nvm bash_completion
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Now your .bash_profile file looks like as following
export NVM_DIR="/Users/test/.nvm"# This loads nvm
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm bash_completion
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
press ^O(ctrl+o) and hit enter to save file. then ^X (ctrl+x) to close the nano editor
Just for confirmation file is saved try to reopen .bash_profile file you will see your changes as following screenshot.
Now check NVM is installed or not by following command in the terminal.
nvm
You will see the above screenshot will give you error
-bash: nvm: command not found
Try to close or create new terminal then enter the same command
nvm
Now you will see nvm is installed as following screenshot.
Now its time to install Node.Js using NVM
You can check different versions of the Node.JS by following link
We will install node using NVM
First try run following command
nvm list
It will return you the list of installed Node.JS versions as following screenshot
You can see there is no installed version of Node.JS in above screenshot
Now run the following command
nvm install node
it will install default/latest version of the Node.JS (v12.4.0)
You can install another version of node as well for example
nvm install v6.17.1
Now you can see we have 2 different versions of Node
v6.17.1
v12.4.0
you can switch to different version of node for example
nvm use v6.17.1
nvm use v12.4.0
Conclusion
Most of the Web apps, Microservices, REST API’s etc. Now days using Node.JS (Javascript Stack) and We cannot guarantee that each of these services, apps are using the same version of Node.JS. To manage these different version of Node.JS in the local machine have to use Node Version Manager (NVM).