Tutorials for web designers and developers
Tutorials for Developers_
Close this search box.

Tutorial 1: Introduction to Node.js | Node.js Tutorials

Hafiz Faraz Mukhtar

Hafiz Faraz Mukhtar

Hafiz Faraz Mukhtar is an expert Web Developer, SEO specialist, and Graphic Designer. Hafiz Faraz is an expert in WordPress design and development. Hire on Upwork | Hire on Fiverr

Welcome to Node.js Course. This is first of many node.js tutorials. This post describes what node is, what node is not, practical application of node.js, why use node, who should learn node, what you should know before getting started, installing node and running your first node program. Moreover, we will also look into modules, how to install third-party modules in node.js, how to uninstall and update local and global modules.

At the end of this post, you will end up making your own module in node.js whether you are using windows operating system, MAC or using GIT bash. If you haven’t used GIT bash, you may install it from here, although its not necessary but if you are used to UNIX syntax then you must download it. So lets dive in.

What is Node.js?

Its JavaScript without the browser. It is for front-end plus back-end development which allows you to build scalable network applications.

What Node.js is NOT?

  1. Not a framework
  2. Not for Beginners because it is very low level
  3. It is not multi-threaded(think as single threaded server)

Practical applications of node.js

  • Browser based chat application
  • Real time statistical data application e.g money exchange rates
  • Fast data(pictures, documents etc) access from server
  • scoring application (say for cricket scoring) where lots of people accessing change in score every split second.

Node.js is for:

  1. Front-end engineer, who is interested to apply front-end skills to server-side development.
  2. Server-side engineer, who uses PHP, Ruby, Java, python or .Net

Why use Node.js

  1. Extremely fast: It runs on top of Google’s V8 JavaScript engine, which makes it several times faster than ruby and python.
  2. Non-blocking: It is asynchronous programming: (faster than synchronous programming)
    • Do not wait for line  icon-arrow-right  move to next line  icon-asterisk  when I/O returns  icon-arrow-right  trigger to callback function  icon-arrow-right result
  3. Simple: Core functionalities are kept to a minimum with minimum amount of complexity to the programmers.

Before learning Node.js, you should be familiar with:

  1. Javascript. Node.js is a complete javascript environment and familiarity is essential.
  2. General concepts of how TCP and HTTP works.
  3. Familiarity with command line interface will be helpful.
  4. Classic web development (HTML, CSS, JS)

Installing node.js

There are three ways to install node.js:

  1. Pre-compiled binaries: Goto node.js icon-external-link  and click install. (easiest way of installation to get started)
    • Windows:you will get node-v*.msi Windows installer. simple double click installer and click next next to install.
    • Mac:you will get node-v*.pkg Macintosh installer. simple double click installer and click next next to install.

    icon-plus  Easy: wizard based installation
    icon-plus  Automatic: configures environment for you
    icon-minus  Update requires reinstallation
    icon-minus  Switching requires reinstallation

  2. Using version manager: Goto node version manager (nvm)  icon-external-link  and click install.
      Installs any version quickly
      Switch versions quickly
      Easily installs on Linux
      No installation wizard available: must use command line interface
      Reinstall global modules when switching versions (talk about it in later tutorial)
      You need to install version manager first, then node can be installed
  3. By compiling source: Goto github hosted code  icon-external-link  and click install.
    • UNIX: 
      1. Choose node version you will be installing, stable versions have an even minors (0.2, 0.4, 0.6), and unstable versions have an odd minors (0.1, 0.3, 0.5, 0.7)
      2. Copy the code tarball URL from the Choose node version website and download it
      3. on Unix you probably wget installed which means that you can download it by using a shell prompt and typing the following: $ wget http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz If you don’t have wget installed, you may also use the curl utility: $ curl -O http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz
      4. Build node: unpack source file like: $ tar xfz node-v0.6.12.tar.gz
      5. Access source directory:  $ cd node-v0.6.12
      6. Configure:  $ ./configure
      7. You get successful output: ‘configure’ finished successfully (9.278s)
      8. Compile:$ make
      9. Success: ‘build’ finished successfully (0.734s)
      10. Installation: $ sudo make install
      11. Run: $ node

      Here you get latest code
      With this you can customize the installation
      You must be familiar with compilation process
      Takes longer to install
      Switching between node versions is not easy (requires recompilation)

Running your first node program

open node.js and in its console write:

 console.log("hello hfarazm");

you will get :

yeah! That is your first successful attempt. Now lets do something more.

Running JavaScript file in node.js

WINDOWS: Now lets see how to execute js file using node in windows specifically.

  • First, create a simple JavaScript file holding following line of code and name file as test.js:
    console.log("hi from test file");
  • Second, place the test.js file where you installed node. In my case, I installed node in D:/Program Files/nodejs/ and placed test.js file inside it. Your installation directory might be different from mine. In windows, open command prompt using  icon-windows + R and write cmd to open command prompt. You will see something like this:
  • Third, change directory to where you installed node. For example, if you installed node in D:/Program Files/nodejs then you simply write in console D: to change drive from c to d. Then navigate to specific directory like: cd Program Files/nodejs. I did it like this: 

MAC or GIT BASH: you can read js file in node using mac or git bash like this:

  • First, open terminal in MAC or GIT BASH and navigate to directory where you installed node. In my case, I installed at D:/Program Files/nodejs. So I type in cd D:/Program Files/nodejs ( is escape character used when there is a space between name and then we write space to let terminal know about name with space, otherwise it will through error.
  • Second, in mac terminal or in Git bash, write: node test.js (make sure you have place test.js file where you installed node)This was pretty simple. Lets create a module in node.js

How to create module in node.js

When you are creating a large application of any sort, you have to break it down into logical pieces. In Node.js these logical piece are called modules. Lets see how we can create modules in node.js:

  • First, create a module.js file inside node.js installation directory and enter the following code into module.js:
    var say = require('./say');
    say('Welcome to hfarazm node.js tutorials');

    Code Explanation: Line 1 says: get a file/folder of name “say” from the same directory where we have placed module.js. Once code grabs the “say.js”(will create this file in next step) file, place contents of say.js in “say” variable.  Line 3 says: simply pass the string to say function(next we are creating it).

  • Secondly, create another file say.js and add the following code to it:
    module.exports = function (msg)
    	console.log('saying: ' + msg);

    Code Explanation: Line 1 says: This is a module which will export the function when required.  Line 3 says: show message to the user. Okay, let me clarify more, module.js will grab the function from say.js file then module.js will pass the value to that grabbed function.

  • Third, place these two file in node installation directory. Then open command prompt(Windows users)/terminal(MAC or Git bash users) and navigate to installation directory which in my case is D:/program files/nodejs and then open module.js by writing in cmd/terminal:
    node module.js
  • Result: Saying: Welcome to hfarazm node.js tutorials
    Thats it you just created a module and loaded it using node.

Well you just created your own module for node.js that shows you simple string message. What about third-party modules? Third-party modules are those which are available for you to use in your project. Thats great, its just like an add-on, with which you can enhance capabilities of your node application with ease. but how to use third party modules? we use NPM to use third party modules.

Node package manager (npm)

Now lets talk about NPM, Node package manager  comes with node.js installation. NPM allows you to download, install, and manage third-party modules in node.js. It has registry service that houses all packages that people publish at NPM. Before installing, updating and removing packages using NPM, it is important to know that NPM has two modes:

  1. Local mode(default):  NPM works on the local directory level, never making system-wide changes
  2. Global mode: suitable for installing modules that always available globally

 Installing third-party module in node.js

  • First, open command prompt and navigate to directory where you installed node.
  • Second, for LOCAL installation of module, write: npm install <package name> specifically like this:
    npm install sax

    for GLOBAL installation of module, write: npm install -g sax
    Code Explanation: sax is the name of module and -g represents a global installation.

 Installing specific version of third-party module in node.js

  • First, open command prompt and navigate to directory where you installed node.
  • Second, for LOCAL installation of module, write: npm install <package name>@<version spec> specifically like this:
    npm install sax@0.2.x

    for GLOBAL installation of module, write: npm install -g sax@0.2.x
    Code Explanation: sax is the name of module we want to install for now and -g represents its a global installation.

 Uninstalling third-party module in node.js

  • First, open command prompt and navigate to directory where you installed node.
  • Second, for LOCAL uninstallation of module, write: npm uninstall <package name> specifically like this:
    npm uninstall sax

    for GLOBAL uninstallation of module, write: npm uninstall -g sax
    Code Explanation: sax is the name of module and -g represents a global uninstall.

 Updating third-party module in node.js

  • First, open command prompt and navigate to directory where you installed node.
  • Second, for LOCAL update of module, write: npm update <package name> specifically like this:
    npm update sax

    for GLOBAL update of module, write: npm update -g sax
    Code Explanation: sax is the name of module and -g represents a global update.

In the next tutorial we will do something more interesting.

Hope you like this post. Please share. Thanks.

next-AngularJS MVC

Leave a Reply

Your email address will not be published. Required fields are marked *