How to Install Node Js?
Video Tutorial
Overview
Node.js is an open-source and free-to-use server-side runtime environment built on Chrome’s V8 JavaScript engine. It is used in the backend development of various web applications. Its purpose is to execute the JavaScript code outside the browser window. We use npm (Node Package Manager) for handling various dependencies of our application.
Scope
This article will discuss how to install Node js and Set up a Node.js development environment.
Pre-requisites
Before moving ahead, make sure you know the basics of JavaScript and backend web development like HTTP requests, and what is a web server and databases. You should also know the normal software installation process. It will be great if you have basic knowledge of frontends like HTML and CSS, as this can help you in creating basic web pages for displaying data.
What is the Express Development Environment?
When we install node js, npm (Node Package Manager), and/or Express Application Generator (if required), we simply refer to this newly formed development environment as an Express development environment. Remember that we don’t have to install node js and npm separately, we just need to install node js as it already includes npm.
Supported Operating Systems
Nodejs is supported on all the major operating systems out there. Specifically, Windows, Mac OS, and many Linux distributions support Node.js. As Nodejs is supported, so are npm and Express.js (Express is a backend web application framework for Node.js).
Databases and Other Dependencies
There are many other dependencies and databases (for example, My SQL Server, Oracle, PostgreSQL, MongoDB LevelDB, etc.) like authentication engines, database drivers, etc., which are required in the application and they can be imported into the application development environment with the help of npm (Node Package Manager).
Install NVM (Node Version Manager)
Before we install node js and npm, let's know this specific term called "nvm" for installing node js which is nothing but a command-line tool known as Node Version manager. When we want to use different versions of node js at different times, we can use nvm. nvm allows this exceptional facility of installing and using different versions of nodes very quickly using the command line.
Here is a quick example for you.
Explanation :
- With the first command, we installed the node of version 12 using nvm.
- With the second command, we installed the node of version 16 using nvm.
- With the third command, we installed the node of version 14 using nvm.
- At this point, we have 3 different versions of the node installed on our machine.
- With the fourth command, we verified the current version that is being used.
- With the fifth command, we can see the list of all the node versions that are installed on our machine.
- With the sixth command, we are switching from node version 14 to version 12 using nvm.
- With the seventh command, we again verified the current node version that is now switched from version 14 to version 12.
The key point is that nvm is the most popular way of installing different versions of nodejs, but it is only available for Linux and Mac but not for Windows. If you want to use nvm on windows, then you may go for the alternatives available out there for this purpose. Here we will be having a look at how to install nvm on Ubuntu which is one of the operating systems based on Linux.
Installing NVM on Ubuntu.
For installing nvm on Ubuntu, first, you will need to have curl ( a command-line tool used to transfer data to and from the server) installed in your system. You can run the following command in the terminal to install curl on your machine.
Once curl is installed, run the following command to install nvm. (curl or 'Client URL' is a command-line tool used for transferring the data to the servers and from the servers. Hence we are using it to install nvm.)
After running the above command successfully nvm should be installed on your machine. Now you can install node js directly from the command line. For that run the following commands in the terminal.
Install Node.js and NPM (Node Package Manager)
The easiest way of installing Node and npm together is using the official website of Node.js. Here we will be taking Windows into consideration for demonstrating the node installation. This process is more or less similar for Mac OS as well as Linux-based OS.
Installing Node.js and NPM in Windows, Mac OS, or Ubuntu.
You just need to go to the official website of nodejs and choose your Operating System. Reference image below.
Here you can see that two options are available for downloading the node.js. For most of the users, it is recommended to use LTS i.e., Long Term Support version, as it is a stable release. You can also use the current version if you want to use some specific feature that is only available in the current version and not in the LTS version. After choosing the version, just click on it and follow the basic installation process as you do while installing other software and stuff like that. Simple as that.
Note: You can also install node js directly from the Ubuntu repositories but it is not recommended as they contain very old versions of node js.
Testing/verifying your Nodejs and npm installation
To check if the node.js is properly installed on your machine or not, run the following command in your command prompt or the terminal in mac/Linux.
It should return the installed version of node js in your machine. ( If you are not getting the installed version, then you should try troubleshooting the errors(if any) and/or install it again )
As mentioned earlier, npm also gets installed with node. You can check or verify this by running the following command in your command prompt.
If you want to install the most recent LTS version via a node package manager, then you can follow the below-given steps. We will be using n module for managing node js versions from the command line itself.
- Clear the cache by running the following command.
- After clearing the cache, install n to manage node js versions.
- Now, we can install the most recent version of node js via npm by running the following command.
- You can also install the most recent current version of node js via npm by running the following command.
Using npm (Node Package Manager)
We can use npm to manage the dependencies (the modules/packages required for our project to work properly) for our local projects. npm has the ability to install all the dependencies that are required in the application as a package in just a single command.
npm (node package manager) is used to manage the packages, and npm acts as the source for those packages. We can get any package that is present in npm and use it in our application or project whenever necessary.
We can separately fetch the needed dependencies individually from the node, but we often use a specific file called package.json (a plain text-formatted file) because it contains all the dependencies of a specific package that are required to run the specific application and makes it easier to handle the dependencies.
Adding dependencies
Now we will see how to download the package using npm and save the downloaded package in our project dependencies and how we can require the downloaded package so that we can use it.
- Make a new directory and navigate to it using the commands given below.
- After that, use the npm init command given below to create or initialize the project.json file in the new project. Accept all the prompts like name, version, etc., that you are getting. You can add or change them later also if you want to.
(npm init command is used for the setup of the existing npm package or the new npm package)
- Once the process is completed, run the following command to see the package.json file of your node application.
- If you want to install Express.js then you can run the following command.
- Once express gets installed, package.json will look something like this.
- We call the "require()" function in the index.js file when we want to use the express. Refer to the below given example.
- To start the server, we call the node with the script (here index.js) as given below. It will return the following string.
- You can go to this link http://127.0.0.1:3000/ . You will see "Hey! it's my new application" when your program runs successfully.
Development dependencies
When there is any dependency in your application that is used only when the development is going on, then instead of installing it globally, you should save it as a development dependency. You can see that in the below-given command, we are installing a JavaScript linting tool called eslint using this way. Its main advantage is that the user is not required to install it again in the production phase.
After running the above command, your package.json will now contain an additional entry like the one given below.
Running tasks
Consider a situation where you have some scripts to run in your command line, there's no problem if those scripts are short in length but the main problem occurs when those scripts are very lengthy. Hence to make it even easier we can define the names of those scripts in the package.json file of our application and call npm for their execution using the run-script command. Have a look at the below code example. As we have installed eslint now, we are defining named scripts for eslint.
we can run it by calling it using npm.
In the above example, the eslint’s script was not lengthy hence there is just a minute difference, but while having lengthy scripts, named scripts are very useful.
Install an editor
We need a text editor so that we can write our code in it. There are many modern text editors like Visual Studio Code, Sublime Text, Vim, etc. that are bundled with powerful features. You can go with any text editor of your choice. Here we are installing VS Code.
Steps to install VS Code
The easiest way to install VS Code is using the official website i.e., code.visualstudio.com/download. Here we will be taking Windows into consideration for demonstrating the VS code installation. This process is more or less similar for Mac OS as well as Linux-based OS.
You just need to go to the official website of VS Code and choose your Operating System. Reference image below.
After choosing your os and preferred version, just follow the typical process of installation for the software on your specific operating system, be it windows, mac, or Linux-based os. Once the text editor is downloaded, just do the basic setup, and you are good to go.
Setting up Node JS environment on cloud
Google Cloud offers the tools to developers that are used to build, deploy, debug, and monitor Node.js applications. Google Cloud has the ability to run the node.js applications end to end. It has libraries optimized for Node.js and many such features.
Once you have installed node, npm, and a text editor, you are ready to install the Cloud SDK. (in case you have not installed node yet, refer Install Node.js and npm section of this article)
Install the Cloud SDK
While working with google cloud we require some tools that make our work easier and these tools are provided by the Cloud SDK.
For eg. When you want to access the Compute Engine, BigQuery, Cloud Storage, etc. from your command line, Cloud SDK provides tools like gsutil, bq, gcloud, etc.
The command given above can be used to deploy any of your Node.js web apps to the App Engine standard environment. As the deployment of your app finishes, the App engine itself tries to start your app using the npm start command.
Install the Cloud Client Libraries for Node.js
When the Node.js developers want to integrate their application with various Google cloud services like Cloud Storage and Datastore, they can use Cloud Client Libraries for Node.js for this purpose.
For eg. If the developer wants to install the package for separate APIs, the following command can be used.
Note: Authentication is mandatory to use this client library. You may click here to get started with the authentication process.
Express Application Generator (EAG) Installation
To generate or create an express application structure or skeleton quickly, we can use the Express Application Generator tool.
The Express Application Generator allows us to generate an application structure which is already configured for the use of some of the most common CSS stylesheet engines like SASS, Compass, LESS, etc.
The below mentioned command is used to generate the express application basic folder structure using npm.
Let us create an express app to understand the working of Express Application Generator properly.
- To create an express app named myapp, we run the command given below.
Note:
If the node js version installed in your machine is greater than v8.2.0, you can directly run express-generator without installing it using npx by running the following command.
but note that since you are just running the express-generator and not installing it, express cannot be called from anywhere.
If you want to play with the other settings of express, you can run the following command.
Once the app creation is completed. The generator tool will show some commands that you have to enter to change the directory, install the dependencies, and run the app.
Now you can see the package.json file in the root directory of your freshly generated express app that will look something like this
Run those commands in proper sequence as shown by the generator tool. Firstly, you have to install the dependencies that are required for myapp using npm.
after installing the dependencies successfully, run the express app using npm command
Debugging: A process used for the detection and removal of 'bugs' or say errors from the code is called Debugging.
In the below code, debug command is used to create useful logging, and it gives the below-mentioned output in the command line.
Now you can see the default Express welcome page in your browser on this address http://127.0.0.1:3000/ .
Congratulations!
Your Node.js / Express.js development environment is ready to create awesome applications.
FAQs
Q1. How to install Node.js?
Ans: Node.js is a server-side runtime environment built on Chrome's V8 JavaScript engine. To install Node.js, you can visit the official website of Node.js and choose the appropriate version for your operating system. Follow the installation process provided on the website to install Node.js on your machine.
Q2. How to install Node.js in Ubuntu?
Ans: To install Node.js in Ubuntu, you can use the Node Version Manager (NVM) tool. First, make sure you have the curl installed on your system by running the command "sudo apt-get install curl" in the terminal. Once curl is completely installed, you can use the following command to install NVM using: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash After NVM is installed, you can install Node.js by running the command "nvm install node" in the terminal. This will completely install the latest version of Node.js.
Q3. How to install Node.js in Windows?
Ans: To install Node.js on Windows, you can visit the official website of Node.js and choose the appropriate version for Windows. Download the installer and follow the installation process provided. After the installation is 100% completed, you can verify the installation by opening an command prompt and running the command "node -v". It should display the installed version of Node.js.
Q4. What is the Express development environment?
Ans: The Express development environment refers to the setup that includes Node.js, npm (Node Package Manager), and Express Application Generator. When you install Node.js, npm is automatically included. Express Application Generator is an optional tool that can be installed separately if needed. This environment is used for developing backend web applications using the Express.js framework.
Conclusion
- From the definition of Express development environment to its installation, you got to know many things like node, npm, and different commands of npm.
- You got to know that nvm can be used to switch between different node versions.
- You got to know about dependencies and databases.
- You also got to know that nvm does not support the Windows operating system.
- You have seen the installation process for the text editor (here VS Code).
- You have seen how to install and set up node.js on different operating systems.
- You have seen how to set up the node.js environment on the cloud and also learned about the Express application generator tool.
- You have also seen the creation of a basic express web application.
- Hope this article has provided you with the information regarding installation and setting up the node.js development environment.