Simple reference to install Node.js
Node.js, as per the basic definition found on nodejs.org, About Node.js page, Node.js, as an asynchronous event-driven JavaScript runtime, is designed to build scalable network applications.
This is a simple guide to downloading and installing Node.js on your machine. Although it is not intended to be a Node.js tutorial, I will be showing you a small example of some node.js applications.
Let’s start with the download and installation first.
Download from Node.js website:
To download node.js, go to https://nodejs.org/en/. The page will offer you two options suitable for your OS, the “Recommended for most users” version and the “Latest Features” version.
You may want to install a specific version of node.js. In that case, clock “DOWNLOADS” from the menu option and look for “Previous Releases” link. There, you can look for the version you desire to download.
In this guide, we will download the “Recommended For Most Users” option. So let’s click it and let it download.
Install Node.js:
Once the installation file download is complete, run it and follow the installation instructions.
Accept the license agreement:
Select the installation path:
Select the installed features. I don’t recommend changing anything in this screen unless you are sure of what you want:
Tool for Native Modules. Also, click Next unless you want to tick the option on this screen.
Ready to install. Click “Install” and wait till it completes the installation process.
And that’s it!
Now that you have installed node.js on your machine, let’s head to the command prompt console and let’s do some checks. Let’s start by checking the node version. On the command prompt run:
node -v
This will display the node.js version installed.
Node.js comes with NPM (Node Package Manager) which is a console tool to initiate node projects, install node libraries, and run JavaScript applications among other things. Let’s check the version of the installed npm using the following command:
npm -v
You should see something like:
If you need help with the node or npm commands try the below commands:
node -h
npm -h
Now, let’s try a couple of applications.
Example 1: the infamous Hello World application.
Node.js is a JavaScript platform. So, this application will be so simple. It will just display Hello World message on screen.
We will start by creating a .js file. Let’s call it helloworld.js. I recommend using Microsoft Visual Studio Code for this. Make a new folder for node apps and create the file inside this folder. You will need to do that in command prompt. The commands will defer based on your OS.
md nodeapps
cd nodeapps
Once inside the folder, you’ll need to initiate a node package using npm. So let’s run the npm init and accept the default settings.
npm init
Now, in Visual Studio Code, or your preferred text editor, create a new file and call it helloworld.js. The extension is important so make sure you have it.
In side the new file, add the following JavaScript command and save the file.
console.log('Hello World');
That’s it. Now, lets run the application. We do that using the node command.
node helloworld
The node command will run the JavaScript code in the helloworld.js and display the outcome on the console.
Simple, isn’t it!? 😄
Example 2: create a simple HTTP server.
Create another file called server.js and add the following code to it.
This is a simple web server which will launch a listener and will display the message “Hello World!” when navigated to from the browser. Start by running the server from the command prompt the same way you did for the previous example.
node server
You should get:
Notice that the application didn’t terminate and return to the command prompt. That’s because the listener will continue listening until you terminate it using CTRL+C or in case an unhandled syntax error appears.
Now, head to your browser and type the following URL: http://localhost:3000. You should get something like this:
This was a very simple introduction to node.js. I hope it was useful.
Thanks for your reading. Don’t forget the 👏 👏 👏 😄