Node ~ Modules - rohit120582sharma/Documentation GitHub Wiki

Module in Node.js is a simple or complex functionality organised in single or multiple JavaScript files which can be reused throughout the Node.js application.

Each module in Node.js has its own context, so it cannot interfere with other modules or pollute global scope. Also, each module can be placed in a separate .js file under a separate folder.

We can access the global object in node using the global keyword. The global object exposes a variety of useful properties about the environment. Also this is the place where properties as process, console, setImmediate() and clearTimeout() are located.

Module Types

  • Core Module: Node.js is a light weight framework. These modules include bare minimum functionalities of Node.js. These core modules are compiled into its binary distribution and load automatically when Node.js process starts. However, you need to import the core module first in order to use it in your application.
  • Local Module: These are modules created locally in your Node.js application. These modules include different functionalities of your application in separate files and folders. You can also package it and distribute it via NPM, so that Node.js community can use it.

Import Module

To use any modules in your application, you need to load it using require() function. However, you need to specify the path of JavaScript file of the module.

The module.exports object in every module is what the require() function returns when we require that module.

The whole process of requiring/loading a module is synchronous.

When Node invokes that require() function with a local file path as the function’s only argument, Node goes through the following sequence of steps:

  • Resolving: To find the absolute path of the file.
  • Caching: It checks whether the module is in the cache already - if so, it returns the exports object. Otherwise, it creates a new module for the file and saves it to the cache. So that when we require this file again, we don’t go over all the steps another time.
  • Loading: To load the contents and determine the type of the file content.
  • Wrapping: To give the file its private scope. This is what makes both the require and module objects local to every file we require. This is why you can access the global-like variables like require and module. It also ensures that your variables are scoped to your module rather than the global object.
  • Evaluating: This is what the VM eventually does with the loaded code.
var greetModule = require('./greet');

Export Module

The module.exports or exports is a special object which is returned in every JS file in the Node.js application by default.

The module is a variable that represents current module and the exports variable inside each module is just a reference to module.exports which manages the exported properties.

Use module.exports or exports to expose a function, object or variable as a module in Node.js.

/**
 * greet.js code
 */
var name = '';
module.exports = {
	setName: function(val){
		name = val;
	},
	greet: function(){
		console.log('Hello ' + name + '!');
	}
};

/**
 * Node wraps 'greet.js' code into a wrapper function and passes this wrapped code to V8 engine for compiling
 * Require function returns <module.exports> an empty object
 */
/*
(function(exports, require, module, __filename, __dirname) {
	var name = '';
	module.exports = {
		setName: function(val){
			name = val;
		},
		greet: function(){
			console.log('Hello ' + name + '!');
		}
	};
	return module.exports;
})(module.exports, require, module, filename, dirname);
*/
⚠️ **GitHub.com Fallback** ⚠️