Core Modules

Node.js has several modules compiled into the binary. The core modules are defined within Node.js’s source and are located in the lib/ folder.

Core modules are always preferentially loaded if their identifier is passed to require(). For instance, require(‘http’) will always return the built in HTTP module, even if there is a file by that name.

Node.js Core Modules

Node.js is a light weight framework. The core 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. The following table lists some of the important core modules in Node.js.

Core Module Description
http http module includes classes, methods and events to create Node.js http server.
url url module includes methods for URL resolution and parsing.
querystring querystring module includes methods to deal with query string.
path path module includes methods to deal with file paths.
fs fs module includes classes, methods, and events to work with file I/O.
util util module includes utility functions useful for programmers.

Loading Core Modules

In order to use Node.js core or NPM modules, import it using require() function as

var module = require(‘module_name’);

As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.

The following example demonstrates how to use Node.js http module to create a web server.

Example: Load and Use Core http Module

var http = require(‘http’);

var server = http.createServer(function(req, res){

//write code here

});

server.listen(5000);

In the above example, require() function returns an object because http module returns its functionality as an object, you can then use its properties and methods using dot notation e.g. http.createServer(). In this way, you can load and use Node.js core modules in your application.

Get industry recognized certification – Contact us

Menu