Node.js quick start for Deep dive — Part 1

I have been learning Node.js for few months. Initially, I found that the docs had more of API modules briefly explained and going through all was not necessarily needed for starters, So I pulled out few notes on some specific modules.
The best way to learn NodeJs is through REPL. Typing node in your CLI will get you into REPL. Taping on TAB key twice gets the auto-suggest displaying all modules and API available in Node.js.

require root setImmediate setInterval setTimeout
stream string_decoder tls tty unescape
url util v8 vm zlib

let’s start on to cover with few of these.


All files in Node.js have to be loaded first using the require() which is available in global. Calling the require on core modules gets loaded immediately from the node itself.For a non-core module, the require() searches through the file specified in the list provided by the module’s paths

Module {
id: '<repl>',
exports: {},
parent: undefined,
filename: null,
loaded: false,
children: [],
[ '/home/nivendha/Desktop/learnNodeJs/repl/node_modules',
'/home/nivendha/.nvm/versions/node/v7.8.0/lib/node' ] }

Files are searched hierarchically from the current directory way down to the root directory. If it’s not able to locate the file, then we will get a Cannot find module error trace.

Adding a package.json to your local folder, you can load it just by giving the folder path.

>require('./src/code') //loads the run.js file

The modules have an one-to-one correspondence in Node. Requiring a module/file goes through the following process.

  • Resolving
    In this the absolute path of the file is located. This can also be used to check if the file exist in case of optional dependency
nivendha@IMCHLT116:~$ node
>require('./index.js'); //Loads index.js file and executes it.
>require.resolve('./index.js'); // Returns the resolved filename
  • Wrapping
    Now the module/file is wrapped around a wrapper function, thus providing a scope for the module.
  • Evaluating and Caching
    Now the file gets loaded and cached for any future exports, next call to require loads the cached file.


This plays the bread and butter for all Classes extending the EventEmitter. It provides the ability in Node.js to extend event listener for objects.

class MyEmitter extends EventEmitter {}const myEmitter = new MyEmitter();myEmitter.on('myEvent', (message) => {
//code to be executed when 'myEvent' is triggered
myEmitter.on('myEvent2', (message) => {
//meesage:"myEvent2 triggered"

The on() method takes an eventName and listener function to be triggered when the corresponding event is raised.

myEmitter.once('myEvent2',"myEvent2 is triggered");
// after the first time of event trigering it gets unregistered
myEmitter.removeListener('myEvent');// unregisters the event

The listeners are executed synchronously in order which they were registered. To switch to an asynchronous mode of operation we can use the setImmediate() or process.nextTick() wrapped over our callback code. The this reference inside the listener is set to EventEmitter.

Upcoming modules

  1. FS module
  2. OS module
  3. Util module
  4. Child Processes

#devloper ,likes art, origami and instrumental music

#devloper ,likes art, origami and instrumental music