Thursday, September 1, 2022

NodeJS Installation using npm

 NodeJS

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine (i.e. V8 engine) and executes JavaScript code outside a web browser, which was designed to build scalable network applications. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts.

The Node.js distributed development project was previously governed by the Node.js Foundation and has now merged with the JS Foundation to form the OpenJS Foundation, which is facilitated by the Linux Foundation's Collaborative Projects program.

npm

npm(Node Package Manager) is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages called the npm registry.


For installing Node.JS and npm we've different methods which are as follows:-

Method 1:- Installing Node.JS and npm using CURL

Method 2:- Installing Node.JS and npm using NVM(Node Version Manager)

Method 3:- Installing Node.JS and npm using Ubuntu Repository


Method 1

Installing Node.js and npm from NodeSource

To install Node.JS and npm you may have to follow the following steps:-

Step 1:- Enable the NodeSource repository by running the following curl command with having sudo privileges :

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

note If you need to install another version,  for example 14.x, just change setup_12.x with setup_14.x


Step 2:- Once the NodeSource repository is enabled, install Node.js and npm by typing:

sudo apt install nodejs

This nodeJs package may contain both the node and npm binaries file.


Step 3:- To verify that you have successfully installed Node.JS and npm or not. Just run the following command :

node --version or node -v

This command may print the installed node version.


Method 2

Installing Node.js and npm using NVM

NVM(Node Version Manager)

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. With NVM you can install and uninstall any specific Node.js version you want to use or test.


To install Node.JS and npm using NVM just follow the following steps:-

Step 1:- Install NVM(Node Version Manager):- 

To Download or install the NVM script run the following command:-

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

After installing the nvm you should close and reopen the Terminal.

To verify that you have successfully installed NVM run the following command:

nvm --version or nvm -v

Step 2:- Installed Node.JS and npm

Now the NVM is installed on your machine you can install the latest available version of the NodeJs. With the help of NVM, you can install different versions of NodeJS on your machine.

To install NodeJs using NVM run the following command:-

nvm install node

Once the installation is completed, verify it by printing the nodeJs version.

Let's install two more different versions, the latest LTS version, and version 12.14.1

nvm install --lts

nvm install 12.14.1


To list installed Node.JS version using NVM just run the following command:-

nvm ls


Method 3

Installing Node.JS and npm using Ubuntu Repository

Step 1:- To install NodeJS and npm run the following commands:-

sudo apt update

sudo apt install nodejs npm

Once the installation is completed, verify it by printing the nodejs version.



Uninstall Node.JS

For Installing Node.JS and npm using Ubuntu Repository.

Installing Node.JS and npm using Ubuntu Repository

If due for some reason you want to uninstall Node.Js and npm packages, you can use the following command:

sudo apt remove nodejs npm


If you've installed NodeJs using NVM then follow the following command:-

Run the command nvm uninstall with the version of Node you’d like to remove. You cannot remove a version you are currently using, so you must switch to a different version first.

nvm uninstall 12.14.1


NVM Uninstall Steps

If you want to remove NVM also then please follow the following steps:-

Step 1:- Deactivate NVM using the following command:

nvm deactivate

Step 2:- Uninstall NVM

nvm unload

Step 3:- Clear up your .bashrc file by removing the following lines:-

export NVM_DIR="$HOME/.nvm"

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm

No comments:

Post a Comment