Node.js Succinctly: Introduction to Node.js
What Is Node.js?
According to the Node.js website, “Node.js is a
applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and
efficient, perfect for data-intensive real-time applications that run across distributed devices.” I
suggest you also read the “What is Node.js” thread for a more in-depth Node.js definition.
Node.js provides the complete solution for server-side applications, such as web platforms. It
can communicate with other systems, like database, LDAP, and any legacy application. There
are many scenarios we can implement for Node.js.
Node.js can run on Windows, Linux, and Mac. It provides 32-bit and 64-bit platforms. To install Node.js for Ubuntu Linux, use the console and write the following script:
sudo apt-get install python-software-properties sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs npm
For complete instructions on how to install Node.js for another Linux platform, visit GitHub.
If you are a Windows user, you can install Node.js using the setup file. You can download it
from the Node.js website. Download the .MSI file according to your
platform. Run it and you will get the setup dialog as follows:
Follow the installation instructions.
A Node.js icon will be created on the Windows menu after installation. If you click this menu, the
Node.js console will open.
You can run the Node.js console manually from Windows Command Prompt (CMD). Launch it
and type the following:
It will display a response like the Node.js console.
You can use any text editor to write Node.js code. If you want to get more development
experience, you could use the code editor with rich features such as WebStorm JetBrains,
Eclipse, and Visual Studio. Some code editors may provide a debugging feature.
For Visual Studio 2012, you can use a web project template such as ASP.NET Empty Web Site.
Visual Studio 2012 provides an IntelliSense feature. It can help you to display information about
API and syntax.
Learning a new programming language usually starts with writing “hello world” code. Now how
about Node.js? Let’s start writing “hello world” for Node.js.
First, run your code editor and write the following:
￼console.log('Hello world, nodejs');
Save this code to a file named helloworld.js.
Then open CMD or Terminal (Linux) and execute this file:
Here is the sample output of our “hello world” application:
Node.js provides modules to help our development. It is possible to cut development time
because you don’t need write many lines of code.
There are a lot of Node.js modules that you can use. The list of Node.js modules can be seen on this GitHub page.
To install Node.js modules, you need the Node Packaged Modules (npm) package manager.
After installing Node.js, you should have the npm package manager. You can check this by
typing the script shown in the following table:
Then you will get a response on the console as shown in the following figure:
Let’s start to use a Node.js module, for instance, to add color to our console text. The module is
First, install the
￼￼npm install cli-color
Note: To install a Node.js module, your computer must be able to access an internet network. Some modules may require administrator level privileges for installation.
The output of installation is shown in Figure 8.
How do we use this module?
We need call the Node.js module in our code by calling
require with the module name.
var clc = require('cli-color');
Therefore, if we want to color text, we can call
xxx.blue for blue text and
xxx.green for green
xxx is a cli-color object.
console.log(clc.blue('"Hello node.js" in blue')); console.log(clc.red('"Hello node.js" in red')); console.log(clc.green('"Hello node.js" in green'));
The sample of program output can be seen in Figure 9.
Updating Node.js Version
How do you know the version of Node.js you are working with?
You can use a Node.js command to get the information on your Node.js version. Write this
This is the output of Node.js script:
Node.js doesn’t automatically update the existing runtime. You should check and update the
Node.js version manually. Download the setup file from the Node.js website and then install it
on your computer.
The setup installation will check the installed Node.js. If it was found, it will update the current
version of Node.js. Check your Node.js version again after installation is complete.
This tutorial represents a chapter from Node.js Succinctly, a free eBook from the team at Syncfusion.
Source: Tuts Plus