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
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
- Software: Node.js LTS from nodejs.org, VS Code, and a terminal
- Knowledge: Lessons 1–5 in this course
- Previous lesson: Node.js Runtime — Complete Guide
Explain it simply
Modules let you split code into files. You export from one file and require (or import) it in another.
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)
- You write JavaScript in a
.jsfile about Modules. - You run it with
node filename.jsin the terminal. - Node prints output or starts a server depending on the lesson.
- 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
- Create math.js and app.js in the same folder
- Run node app.js — you should see 5
- Add a subtract function and use it
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
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!