codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

4 / 63 topics
1Getting Started with Node.js2Node.js Installation3Hello World Program4Node.js Modules5Node Package Manager (NPM)
Tutorials/Node.js/Node.js Modules
🟢Node.js

Node.js Modules

Updated 2026-05-15
10 min read

Node.js Modules

Introduction

In the world of JavaScript, modularity is a key concept that allows developers to organize code into reusable components. Node.js, being built on top of the V8 JavaScript engine, fully supports this principle through its module system. Understanding how to create and use modules in Node.js is essential for building scalable and maintainable applications.

Concept

Node.js uses a CommonJS module system, which allows you to define and export functions, objects, or primitives from one file (module) and import them into another. This modularity helps in keeping the codebase clean, organized, and easier to manage.

Key Terms

  • Module: A JavaScript file that exports certain variables or functions.
  • Export: The process of making a variable, function, or object available for use in other modules.
  • Import: The process of using an exported variable, function, or object from another module.

Examples

Let's dive into some practical examples to understand how modules work in Node.js.

Creating a Simple Module

First, let's create a simple module that exports a function. Suppose we want to create a utility module for basic mathematical operations.

  1. Create the Module File

    Create a file named mathUtils.js and add the following code:

JavaScript
1// mathUtils.js
2 const add = (a, b) => {
3 return a + b;
4 };
5
6 const subtract = (a, b) => {
7 return a - b;
8 };
9
10 module.exports = {
11 add,
12 subtract
13 };
  1. Using the Module

    Now, let's create another file named app.js where we will import and use the functions from mathUtils.js.

JavaScript
1// app.js
2 const math = require('./mathUtils');
3
4 console.log(math.add(5, 3)); // Output: 8
5 console.log(math.subtract(5, 3)); // Output: 2
  1. Running the Code

    Open your terminal and run the following command to execute app.js:

Terminal
{$ node app.js}

You should see the following output:

Output
8
 2

Exporting and Importing Multiple Items

Node.js also allows you to export multiple items from a module. Let's modify our mathUtils.js file to include more functions.

JavaScript
1// mathUtils.js
2const add = (a, b) => {
3return a + b;
4};
5
6const subtract = (a, b) => {
7return a - b;
8};
9
10const multiply = (a, b) => {
11return a * b;
12};
13
14module.exports = {
15add,
16subtract,
17multiply
18};

In app.js, you can import and use these functions like this:

JavaScript
1// app.js
2const { add, subtract, multiply } = require('./mathUtils');
3
4console.log(add(5, 3)); // Output: 8
5console.log(subtract(5, 3)); // Output: 2
6console.log(multiply(5, 3)); // Output: 15

Using ES6 Modules

Node.js also supports ES6 modules using the import and export syntax. To use ES6 modules, you need to add "type": "module" in your package.json file.

  1. Modify package.json

    Add the following line to your package.json:

JSON
1{
2 "type": "module"
3 }
  1. Update mathUtils.js

    Change the export syntax in mathUtils.js to ES6 style:

JavaScript
1// mathUtils.js
2 const add = (a, b) => {
3 return a + b;
4 };
5
6 const subtract = (a, b) => {
7 return a - b;
8 };
9
10 export { add, subtract };
  1. Update app.js

    Change the import syntax in app.js to ES6 style:

JavaScript
1// app.js
2 import { add, subtract } from './mathUtils.js';
3
4 console.log(add(5, 3)); // Output: 8
5 console.log(subtract(5, 3)); // Output: 2
  1. Running the Code

    Run the following command to execute app.js:

Terminal
{$ node app.js}

You should see the same output as before.

What's Next?

Now that you have a good understanding of Node.js modules, the next step is to learn about managing dependencies in your projects using the Node Package Manager (NPM). NPM allows you to easily install and manage third-party packages that can enhance the functionality of your applications. Stay tuned for more tutorials on this topic!

Info

Remember, mastering modules is crucial for building robust Node.js applications. Practice by creating your own modules and experimenting with different export/import patterns.


PreviousHello World ProgramNext Node Package Manager (NPM)

Recommended Gear

Hello World ProgramNode Package Manager (NPM)