Lesson 6/100

Tutorials Node.js Tutorial

Modules — Complete Guide

Modules — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Node.js Tutorial on Toolliyo Academy.

On this page
Modules
Lesson 6 of 100 · Module 1: Node.js Foundations · BEGINNER
Topic: Modules · Level: BEGINNER · Read time: ~12 min + hands-on

Modules

This lesson covers Modules. Let us learn this step by step — no rush, no jargon first.

What you will learn

  • What modules means — in normal words, not textbook words
  • How it works step by step
  • Code you can run today on your laptop
  • Where teams use this in real projects

Before you start

Explain it simply

Modules let you split code into files. You export from one file and require (or import) it in another.

Think of it like this: A restaurant kitchen: one chef (main thread) takes orders, while helpers (libuv) handle oven timers and deliveries without blocking the chef.

Why developers use this

  • Keep files small and focused
  • Reuse logic across the project
  • Avoid one giant app.js

How it works (step by step)

  1. You write JavaScript in a .js file about Modules.
  2. You run it with node filename.js in the terminal.
  3. Node prints output or starts a server depending on the lesson.
  4. You change one line, run again, and see what changed — that is how you learn.

Code example — type this yourself

// math.js
exports.add = (a, b) => a + b;

// app.js
const { add } = require('./math');
console.log(add(2, 3));

exports.add makes the function available. require('./math') loads it (note the ./ for local files).

What each part does

  • // math.js — Line 1: runs as written.
  • exports.add = (a, b) => a + b; — Line 2: runs as written.
  • // app.js — Line 3: runs as written.
  • const { add } = require('./math'); — Loads a built-in module or package you installed with npm.
  • console.log(add(2, 3)); — Prints to the terminal — great for learning; use proper logging in production.

Real life: where Modules shows up

A startup team uses Modules when they bootstrap their first API. The developer runs a small script on a laptop, stores config in .env, and splits code into modules before the app grows. Start small: one feature working beats a perfect architecture on paper.

Try it yourself — hands-on

  1. Create math.js and app.js in the same folder
  2. Run node app.js — you should see 5
  3. Add a subtract function and use it
Tip: Later you can use import/export (ES modules) — we cover that in the ESM lesson.

Common mistakes (avoid these)

  • require('math') without ./ — Node looks in node_modules, not your folder.

Interview note

Interviewers often ask: “What is Modules?” Answer in one sentence, then give a tiny example you actually ran.

Summary

  • Use module.exports or exports to share code
  • require loads CommonJS modules
  • Local files need ./ prefix

Continue learning

Previous: Node.js Runtime — Complete Guide

Next: File System — Complete Guide

Lesson 6 of 100 · Node.js Tutorial

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Node.js Tutorial
Course syllabus

Node.js Tutorial

Module 1: Node.js Foundations
Module 2: Async Programming
Module 3: Express.js & EJS
Module 4: REST APIs & Databases
Module 5: Real-Time & Event Systems
Module 6: Advanced Node.js
Module 7: Performance & Security
Module 8: Testing & Deployment
Module 9: Latest Node.js Features
Module 10: Enterprise Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details