Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

NodeJS Modules

Modules

NodeJS modules are the set of functions bundled together into single or multiple javascript files, which perform a particular task and represents various functionalities. It has a unique context, thus, it never interfere nor pollute the scope of other modules. NodeJS modules enable the code reusability and enhance the ease of usage. NodeJS modules includes three different types of modules-

  • Core/Built-in Modules.
  • Local Modules.
  • Third Party Modules.

Core/Built-in Modules

As NodeJS is a very lightweight framework so, the core modules bundle the absolute minimum functionalities. NodeJS has a various built-in modules which you can use without any further installation, which are given below-

ModuleDescription
httpIt includes classes, methods, and events required to create NodeJS HTTP server.
urlIt includes methods for URL resolution and parsing.
querystringIt includes methods to deal with a query string.
pathIt includes methods to deal with file paths.
fsIt includes classes, methods, and events to work with file I/O.
utilIt includes utility functions, which can be useful for programmers.

Core module can be easily loaded using below code-

example
const module = require('module_name');

Local Modules

The local modules of NodeJS are custom modules which are created locally by the developer. These modules can include various functionalities of application, which can be bundled into separate files and folders. Local modules can be easily distributed to the NodeJS community using NPM (Node Package Manager).

After creation, a local Modules can be easily loaded like core modules. Let show it, using a basic example-

example
// Create your custom or local module file.
const detail = {
	name: function (name) {
		console.log('Name: ' + name);
	},
	address: function (address) {
		console.log('Address: ' + address);
	}
};
module.exports = detail;

// To use this, include it in your main application file.
const myLocalModule = require('./local.module.js');
myLocalModule.name('Tutorials Logic');
myLocalModule.address('Bangalore, IN');

Third Party or External Modules

example
npm install --g module_name // To install it globally
npm install --save module_name // To install it locally in your application
Key Takeaways
  • Node.js uses CommonJS modules (require/module.exports) by default; ES Modules (import/export) are also supported.
  • require() is synchronous and caches modules - the same module instance is returned on subsequent requires.
  • Use module.exports to export a single value; exports to export multiple named values.
  • Built-in modules (fs, http, path, events) don't need installation - just require them.
  • Third-party modules are installed via npm and stored in node_modules/.
  • ES Modules (.mjs or type:module in package.json) use import/export syntax.



Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.