Modules in Node


As far as Node.js is concerned, you could write all of your JavaScript code in one file. But to humans, it’s really hard to manage thousands of lines of code in a single file. For example, imagine you wanted to reuse a piece of code in another project, but it was buried on line 25,436 of some file. Your only recourse would be to copy that code and paste it into the other project’s file. Modules are an elegant solution to this problem.

In Node.js, a module is just a file that contains JavaScript code. And the module system in Node.js allows you to take pieces of code, split them out into separate files, and easily reuse them in different places.

Learning Objectives:

  • What are Node Modules?
  • Node.js Module Types
  • How to reuse or recall Modules .
  • javascript require vs import.
  • Export object.


What’s a Node.js module?

JavaScript module is a file that contains a few lines of code written in JavaScript. They are the same as JavaScript Libraries. Modules often contain a class or a library of functions that are used for a specific purpose. These can be called with the help of require and import statements. The use of modules reduces the number of lines of code in one’s program/script. A major advantage of modules is that functions of another module can be called without writing the body of the functions themselves. Some of the common modules are: CommonJS, AMD, RequireJS, ES(ECMAScript)6 Modules. Refer to this medium article for an in-depth explanation of how these modules are different.

JavaScript require vs import:

Syntax and explanation:
  1. require():

In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically:

  • reads a JavaScript file,
  • executes it,
  • and then proceeds to return the export object.

require()’s statement not only allows to add built-in core NodeJS modules but also community-based and local modules.


To include a module, the require() function is used with the name of the module:

let myVar = require ('http'); // to use built in module
let myVar2 = require ('./myLocalModule'); // to use local modules
  1. import():

import() & export() statements are used to refer to an ES module. Other modules with file types such as .json cannot be imported with these statements. They are permitted to be used only in ES modules and the specifier of this statement can either be a URL-style relative path or a package name. Also, the import statement cannot be used in embedded scripts unless such script has a type="module". A dynamic import can be used for scripts whose type is not “module”


let myVar = import ('module-name'); 

What are the three kinds of modules?

There are three kinds of modules in the Node.js:

  • Core modules.
  • File modules || Local modules.
  • Third-party modules || NPM modules.
Core modules:

These are the built-in modules in Node.js like fs, http, and path. You require these modules by their name only.

let fs = require('fs');
let http = require('http');
let path = require('path');

Read more about core modules in Tutorials teacher.

Local || File modules:

These are modules that you’ve created on your own (created locally in your Node.js application).

When creating a file module, you add values to a module.exports object using one of the mentioned techniques in the next session. After that your modules will be available to be required.

When requiring a file module, you provide a path to the module, minus the .js extension. These paths must start with /, ./, or ../ to indicate where on the filesystem Node.js can find the file module.

let myModule1 = require('/myModule1');   // absolute path
let myModule2 = require('./myModule2');  // same path as the current module
let myModule3 = require('../myModule3'); // parent path of the current module

Implement the example mentioned in TutorialsTeacher and see how it works. For more practice implement the calculator example .

Third-party modules || NPM modules:

Third-party modules are modules that are available online using the Node Package Manager(NPM). These modules can be installed in the project folder using npm install or globally using npm install -g Some of the popular third-party modules are mongoose, express, angular, and react.


  • npm install express
  • npm install mongoose NPM modules are required just like core modules.
let express = require('express');
let morgan = require('morgan');
let bodyParser = require('body-parser');

Export Module in Node.js:

Here, you will learn how to expose different types as a module using module.exports. The module.exports is a special object which is included in every JavaScript file in the Node.js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module. So, whatever you assign to module.exports will be exposed as a module. Let’s see how to expose different types as a module using module.exports.

Export Literals:

As mentioned above, exports is an object. So it exposes whatever you assigned to it as a module. For example, if you assign a string literal then it will expose that string literal as a module. The following example exposes simple string message as a module in


module.exports = "Hello World"

Now, import this message module in your app.js file and use it as shown below.

let msg = require ('./Message.js')

Run the above example and see the result, as shown below.

c:\> node app.js
Hello World

Note: You must specify ./ as a path of root folder to import a local module. However, you do not need to specify the path to import Node.js core modules or NPM modules in the require() function.

Export Object

The exports is an object. So, you can attach properties or methods to it.

  • The following example exposes an object with a string property in Message.js file.
exports.simpleMessage = "Hello World"
module.exports.simpleMessage = "Hello World"

In the above example, we have attached a property SimpleMessage to the exports object. Now, import and use this module, as shown below.

let msg = require ('./Message.js')

In the above example, the require() function will return an object { SimpleMessage : 'Hello World'} and assign it to the msg variable. So, now you can use msg.SimpleMessage.

Run the above example by writing node app.js in the command prompt and see the output as shown below.

c:\> node app.js
Hello World
  • In the same way as above, you can expose an object with function. The following example exposes an object with the log function as a module.

log.js file

module.exports.log = function(msg){

The above module will expose an object- { log : function(msg){ console.log(msg); } } . Use the above module as shown below.


let msg = require ('./log.js')
msg.log("Hello World")

Run and see the output in command prompt as shown below.

c:\> node app.js
Hello World
  • You can also attach an object to module.exports, as shown below


module.exports = {
firstName : "James",
lastName : "Bond"


let person = require('./data.js');
console.log(person.firstName + '' + person.lastNmae);

Run and see the output in command prompt as shown below.

c:\> node app.js
James Bond


Additional Resources:

In this section you can find a lot of helpful links to other content. This is a supplemental material for you if you want to dive deeper into some concepts.