Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why modules are needed in Node.js
📖 Scenario: You are building a simple Node.js program that calculates areas of different shapes. To keep your code clean and organized, you want to separate the area calculations into a module.
🎯 Goal: Create a Node.js module for area calculations and use it in your main program to calculate areas of a rectangle and a circle.
📋 What You'll Learn
Create a module file named area.js exporting functions for rectangle and circle area calculations
Create a main file named app.js that imports the area.js module
Use the imported functions to calculate and store the area of a rectangle with width 5 and height 10
Use the imported functions to calculate and store the area of a circle with radius 7
💡 Why This Matters
🌍 Real World
In real projects, modules keep code organized and allow teams to work on different parts without conflicts.
💼 Career
Understanding modules is essential for Node.js developers to build scalable and maintainable applications.
Progress0 / 4 steps
1
Create the area.js module with rectangle area function
Create a file named area.js and write a function called rectangleArea that takes width and height as parameters and returns their product. Export this function using module.exports.
Node.js
Hint
Remember to export your function so other files can use it.
2
Add circle area function to area.js
In area.js, add a function called circleArea that takes radius as a parameter and returns the area using Math.PI * radius * radius. Export this function alongside rectangleArea.
Node.js
Hint
Use Math.PI for the circle area calculation.
3
Create app.js and import area.js module
Create a file named app.js. Import the area.js module using require and store it in a variable called area.
Node.js
Hint
Use require('./area') to import the module.
4
Use imported functions to calculate areas in app.js
In app.js, use area.rectangleArea to calculate the area of a rectangle with width 5 and height 10, and store it in a variable called rectArea. Then use area.circleArea to calculate the area of a circle with radius 7, and store it in a variable called circleAreaValue.
Node.js
Hint
Call the functions from the imported area object with the exact arguments.
Practice
(1/5)
1. Why do Node.js developers use modules in their code?
easy
A. To write code without functions
B. To make the code run faster
C. To avoid using variables
D. To organize code into smaller, manageable parts
Solution
Step 1: Understand the purpose of modules
Modules help split code into smaller files, making it easier to manage.
Step 2: Compare options with module benefits
Only organizing code into smaller parts matches the main reason for using modules.
Final Answer:
To organize code into smaller, manageable parts -> Option D
Quick Check:
Modules = Organize code [OK]
Hint: Modules help split code for easier management [OK]
Common Mistakes:
Thinking modules make code run faster
Believing modules remove the need for variables
Assuming modules eliminate functions
2. Which of the following is the correct way to import a module named mathUtils in Node.js?
easy
A. const mathUtils = require('mathUtils');
B. include('mathUtils');
C. import mathUtils from 'mathUtils';
D. use mathUtils;
Solution
Step 1: Recall Node.js module import syntax
Node.js uses require() to import modules in CommonJS style.
Step 2: Match options with correct syntax
Only const mathUtils = require('mathUtils'); is valid Node.js syntax.
Final Answer:
const mathUtils = require('mathUtils'); -> Option A
Quick Check:
Node.js imports = require() [OK]
Hint: Use require() to import modules in Node.js [OK]
Common Mistakes:
Using import without setup (not default in Node.js)
What is the expected output if greet.js exports a function that returns `Hello, ${name}!`?
medium
A. Hello, Anna!
B. greet is not defined
C. undefined
D. SyntaxError
Solution
Step 1: Understand module import and function call
The greet module exports a function that returns a greeting string.
Step 2: Predict console output
Calling greet('Anna') returns Hello, Anna!, which is logged.
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Function call output = Hello, Anna! [OK]
Hint: Imported functions return expected results when called [OK]
Common Mistakes:
Assuming greet is undefined without import
Expecting undefined if export is missing
Confusing syntax errors with runtime output
4. What is wrong with this Node.js code snippet?
const utils = require('./utils');
console.log(utils.add(2, 3));
// utils.js content:
// module.exports = {
// add: (a, b) => a + b
// }
medium
A. The require path should include file extension
B. The module.exports syntax is incorrect
C. No error, code works correctly
D. The add function is not exported properly
Solution
Step 1: Check require path usage
Node.js allows requiring files without extension if .js is default.
Step 2: Verify module.exports and function export
The add function is correctly exported as an object property.
Step 3: Confirm usage in main file
Calling utils.add(2, 3) is valid and returns 5.
Final Answer:
No error, code works correctly -> Option C
Quick Check:
Correct export and import = works [OK]
Hint: Default .js extension is optional in require [OK]
Common Mistakes:
Thinking file extension is mandatory in require
Believing module.exports syntax is wrong
Assuming function is not exported properly
5. You have two modules: math.js exports functions add and multiply, and app.js imports them. How does using modules help when your project grows larger?
hard
A. Modules automatically speed up your code execution
B. Modules let you reuse code and avoid repeating functions in many files
C. Modules prevent any bugs from happening
D. Modules force all code to be in one file
Solution
Step 1: Understand code reuse with modules
Modules allow sharing functions like add and multiply across files without rewriting.
Step 2: Evaluate other options
Modules do not automatically speed up code or prevent bugs, nor do they force single-file code.
Final Answer:
Modules let you reuse code and avoid repeating functions in many files -> Option B