NodeJS — Beginner Guide

Features, Installation steps

Jeyasumangala Rasanayagam
4 min readFeb 27, 2020

What is NodeJS?

NodeJS is an Opensource, cross-platform, server-side run time environment built on Chrome’s V8 engine. NodeJS was developed by Ryan Dahl in 2009 and its latest version is v0.10.36.

It provides a non-blocking I/O and run-time environment for developing highly scalable server-side and networking applications using javascript. These applications can be run within the Node.js run time on Microsoft Windows, OS X, and Linux. It also provides a library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent.

Node.js can be used to implement different types of applications such as web application, command-line application, real-time chat application etc. However, it is mainly used to build networking applications like web servers, similar to PHP, Java, or ASP.N

Features of NodeJS

  1. Cross-Platform
  2. Single-Threaded — It uses a single-threaded model with event looping. It uses a single-threaded program and the same program can provide service to a larger number of requests when comparing with traditional servers like Apache HTTP Server.
  3. Highly scalable — Event Mechanism plays a main role in the scalability. It helps the server to respond in a non-blocking way and this leads to the scalable.
  4. Very Fast — Being built on Google Chrome’s V8 JavaScript Engine, which is very fast in executing JavaScript code.
  5. No Buffering − Node applications never buffer the data. These applications simply output the data in chunks.
  6. Asynchronous and Event-Driven − The APIs of Node library is asynchronous
  7. All APIs of Node.js library are asynchronous, that means, non-blocking.

Negative aspects of Node.js

  1. Not good for heavy computations — Not suitable for multi-threaded programming.
  2. Lack of Robust libraries — Javascript lacks a strong standard library
  3. API is not stable
  4. Asynchronous programming model

NPM

Npm is the world’s largest software registry. This registry contains more than 800,000 code packages.

It is free to use. we can download all npm public software packages without any registration.

Npm means Node Package Manager which was first created as a package manager for Node.js. All the npm packages are defined in files called package.json. This package.json is written using JSON.

NPM includes a Command-Line Client, that can be used to download and install the software.

The main two functionalities of NPM are,

  • The online repositories are provided by npm for node.js packages/modules which are searchable on search nodejs.org
  • Command line utility is also provided by npm to install Node.js packages, do version management and dependency management of Node.js packages.

Download and install Node.js and NPM

In Ubuntu

Step 1: Installing Node.js

  1. First, update the packages index. Then run the following command to install node.js
sudo apt updatesudo apt install nodejs

2. Then verify the version of Node by execute the below mentioned command

nodejs --versionAfter the version of v0.6.3, the NPM comes bundled with Node.js installables.ornodejs -v

3. This will display the version of the node.

v8.10.0

Step 2: Installing NPM (Node.js Package Manager)

  1. Execute the following command to install npm
sudo apt install npm

2. After install the npm execute the below command to verify the installation.

npm --versionornpm -vor npm -version

3. The output the above command is looks like this.

3.5.2

Run the first program in Node.js

Let’s look an example of executing a “Hello world” program in Node.js

I explained this example based on Ubuntu.

  1. Open the Texteditor. Then copied the below set of codes in that editor. Then save this file as helloWorld.js
var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);

2. In terminal, run the below command to execute the helloWorld.js

node helloWorld.js

3. Start your browser, and run it in the address: http://localhost:8080. Output will look like this.

What is Yarn?

Yarn is a new package manager for node. It is developed by Facebook in collaboration with Google, Exponent and Tilde in 2016 October and distributed under the BSD license.

The main reason for the migration from npm to yarn is stability, ease of use of yarn.

Difference between the Yarn and npm

Yarn has some differences from npm.

  1. Yarn caches all installed packages and it installs the packages simultaneously, and that is the reason why Yarn is faster than NPM.
  2. Both yarn and npm download packages from npm repository. By default, yarn generates yarn.lock to lock down the versions of package’s dependencies.

Problems with yarn

  1. There are problems with installing native modules
  2. Yarn doesn’t compatible with any node.js version older than 5

References

  1. https://waverleysoftware.com/blog/yarn-vs-npm/
  2. https://www.pluralsight.com/guides/yarn-a-package-manager-for-node-js
  3. https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

--

--

Jeyasumangala Rasanayagam

Intern- Software Engineer| WSO2, Undergraduate at Sri Lanka Institute of Information Technology