Node.js quick start for Deep dive — Part 1

nivendha@HomePC:~$ node
>
......
require root setImmediate setInterval setTimeout
stream string_decoder tls tty unescape
url util v8 vm zlib

1.Modules

> moduleModule {
id: '<repl>',
exports: {},
parent: undefined,
filename: null,
loaded: false,
children: [],
paths:
[ '/home/nivendha/Desktop/learnNodeJs/repl/node_modules',
'/home/nivendha/Desktop/learnNodeJs/node_modules',
'/home/nivendha/Desktop/node_modules',
'/home/nivendha/node_modules',
'/home/node_modules',
'/node_modules',
'/home/nivendha/.node_modules',
'/home/nivendha/.node_libraries',
'/home/nivendha/.nvm/versions/node/v7.8.0/lib/node' ] }
> require('./helloWorld.js')
Error: Cannot find module './helloWorld.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at repl:1:1
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
# ./learnNodeJs/src/code/package.json
{
"name":"myCode",
"main":"run.js"
}
>require('./src/code') //loads the run.js file
  • 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
#index.js
var message = "Hello World";
module.exports = function(){
return message;
}
nivendha@IMCHLT116:~$ node
>require('./index.js'); //Loads index.js file and executes it.
//{}
>require.resolve('./index.js'); // Returns the resolved filename
//home/nivendha/Desktop/learnNodeJs/index.js'
  • Wrapping
    Now the module/file is wrapped around a wrapper function, thus providing a scope for the module.
(function(exports, require, module, __filename, __dirname) {
var message = "Hello World";
module.exports = function(){
return message;
}
});
  • Evaluating and Caching
    Now the file gets loaded and cached for any future exports, next call to require loads the cached file.

2.Events

const EventEmitter = require('events');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"
});
myEmitter.emit('myEvent',"myEvent is triggered"); //myEmitter.once('myEvent2',"myEvent2 is triggered");
// after the first time of event trigering it gets unregistered
myEmitter.removeListener('myEvent');// unregisters the event

Upcoming modules

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

--

--

#devloper ,likes art, origami and instrumental music

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store