How to include external libraries by requiring modules in node.js?

To include external libraries in Java, we use import statement.

In node.js, we use require keyword. For example,

var http = require(‘http’);

We can also include relative files as shown below:

var myFile = require(‘./myFile’); // loads myFile.js

To install modules from npm(node package manager),

$ npm install express

Modules aren’t automatically injected into the global scope, but instead you just assigned them to a variable of your choice.


You can create your own module as well.

  •  The first approach would be to export a single object:

        var person = { name: ‘John’, age: 20 };

        module.exports = person;

  • The second approach requires adding properties to the exports object: = ‘John';
       exports.age = 20;

Modules don’t share scope, so if you want to share a variable between different modules, you must include it into a separate module that is then required by the other modules. And also modules are loaded once and then are cached by node.

ProsperaSoft offers Node.js development solutions. You can email at to get in touch with ProsperaSoft Node.js experts and consultants.




Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>