modules

What is a Module in Node.js?

Consider modules to be the same as JavaScript libraries. A set of functions you want to include in your application.

Built-in Modules

Node.js has a set of built-in modules which you can use without any further installation.

Include Modules

To include a modules, use the require() function with the name of the module:

var http = require('http');

Now your application has access to the HTTP module, and is able to create a server:

http.createServer(function (req, res){
    res.writeHead(200, {'Content-Type':'text/html'});
    res.end('Hello World!');

}).listen(8080);

Create Your Own Modules

eg:

exports.myDateTime=function(){
    return Date();
};

Use the exports keyword to make properties and methods available outside the module file.

Include your own module

var dt = require('./myfirstmodule');

Notice that we use ./ to locate the module, that means that module is located in the same folder as the Node.js file.

Last updated

Was this helpful?