Node.js Modules

A module is a collection of code located in a file. In this article, we will learn how to include and export a module.

I. Include modules

require() can be used to import many different types of modules.

For example:

  • Import core modules/packages
  • Import npm modules/packages
  • Import single file in a project
  • Import single JSON files
  • Import folders in a project (an alias for importing an index.js in that folder)
const filesystem = require("fs"); // core module
const express = require("express"); // npm module
const server = require("./server"); // server.js file in the same directory
const server = require("./boot/server"); // server.js file with a relative path down the tree
const server = require("../boot/server"); // server.js file with a relative path up the tree
const server = require("/var/www/app/boot/server"); // server.js file with an absolute path
const routes = require("../routes"); // index.js inside routes folder if there's no routes.js file
const databaseConfigs = require("./configs/database.json"); // JSON file

II. Export modules

To export a module, we use module.exports keyword.

module.exports allows us to export code to other programs or other files.

Module.exports is a global property and it’s available across your entire file.

1. Export a single property

// in number.js
const number = [1, 2, 3, 4, 5];

module.exports = number;
// in calculate.js
const data = require("./number");

// Result: [ 1, 2, 3, 4, 5 ]

2. Export more than 1 property

// in number.js
const number = [1, 2, 3, 4, 5];
const name = ["anna", "joe", "barry"];
module.exports = { number, name };
// in calculate.js
const data = require("./number");


// Result: { number: [ 1, 2, 3, 4, 5 ], name: [ 'anna', 'joe', 'barry' ] }

3. Destructuring

A better way to import multiple things from a different file is destructuring.

Destructuring syntax makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

// in calculate.js
const { number, name } = require("./number");


// Result:
// [ 1, 2, 3, 4, 5 ]
// [ 'anna', 'joe', 'barry' ]

III. Node Core Modules

Core modules come with Node.js and don’t need to be installed. They provide low-level functionality and helper methods.

Core modules allow Node.js to work with the filesystem, networking, binary data, streams, spawn external processes, parse query strings, file paths and URLs, and perform other helpful tasks such as creating HTTP(S) agents/clients and servers.

1. Main core modules

List of main core modules:

  • fs: module to work with the file system, files and folders
  • path: module to parse file system paths across platforms
  • querystring: module to parse query string data
  • net: module to work with networking for various protocols
  • stream: module to work with data streams
  • events: module to implement event emitters (Node observer pattern)
  • child_process: module to spawn external processes
  • os: module to access OS-level information including platform, number of CPUs, memory, uptime, etc.
  • url: module to parse URLs
  • http: module to make requests (client) and accept requests (server)
  • https: module to do the same as http only for HTTPS
  • util: various utilities including promosify which turns any standard Node core method into a promise-base API
  • assert: module to perform assertion based testing
  • crypto: module to encrypt and hash information

2. Include core modules

There is no need to install or download core modules.

To include them in your application, use require() and enter the module/package name as the name string.

For example, the following code imports a package named http.

const http = require('http') //replace `http` with the core module you want to use

The package is in

  • the node_modules folder in the root of the project if it’s an installed npm package
  • the system folder if it’s a core Node module (location depends on your OS and how you installed Node)