Node.js / Node.js Modules

Creating and Exporting Custom Modules

This tutorial will guide you through the process of creating your own custom modules in Node.js. We will also cover how to export them for use in other parts of your application.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explores built-in, third-party, and custom modules in Node.js.

1. Introduction

Goal

This tutorial will guide you through the process of creating and exporting your custom modules in Node.js.

Learning Outcomes

By the end of this tutorial, you will be able to:
1. Create your own custom modules in Node.js.
2. Export your custom modules for use in other parts of your application.

Prerequisites

Basic knowledge of JavaScript and Node.js is necessary. Familiarity with ES6 syntax will be beneficial but not mandatory.

2. Step-by-Step Guide

Modules in Node.js are standalone JavaScript files that export functionalities to be used in other files. They help in organizing code into separate parts with a well-defined interface.

Creating a Module

To create a module, we simply create a new JavaScript file and write our module code into it.

Exporting a Module

To make the functionalities available to other files, we use module.exports or exports.

3. Code Examples

Example 1: Creating a simple module

Let's create a simple module that exports a function to add two numbers.

// add.js
function add(a, b) {
  return a + b;
}

module.exports = add;

In this example, we define a function add and then export it using module.exports.

Example 2: Using the module

To use the module we created, we use require.

// main.js
const add = require('./add');

console.log(add(1, 2));  // Outputs: 3

In this example, we import the add module using require and then use it to add two numbers.

4. Summary

In this tutorial, we learned how to create and export custom modules in Node.js. Creating modules helps us organize our code into separate parts with a well-defined interface.

5. Practice Exercises

  1. Create a module that exports a function to subtract two numbers. Use the module in a separate file.

  2. Create a module that exports an object with four functions: add, subtract, multiply, divide. Each function should take two numbers and perform the respective operation. Use this module in a separate file.

Solutions

  1. Subtraction module:
// subtract.js
function subtract(a, b) {
  return a - b;
}

module.exports = subtract;

Use the module:

// main.js
const subtract = require('./subtract');

console.log(subtract(5, 3));  // Outputs: 2
  1. Calculator module:
// calculator.js
module.exports = {
  add: (a, b) => a + b,
  subtract: (a, b) => a - b,
  multiply: (a, b) => a * b,
  divide: (a, b) => a / b
};

Use the module:

// main.js
const calculator = require('./calculator');

console.log(calculator.add(1, 2));       // Outputs: 3
console.log(calculator.subtract(5, 3));  // Outputs: 2
console.log(calculator.multiply(2, 3));  // Outputs: 6
console.log(calculator.divide(8, 2));    // Outputs: 4

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Favicon Generator

Create favicons from images.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help