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
CommonJS require and module.exports
📖 Scenario: You are building a simple Node.js app that uses CommonJS modules to organize code. You want to create a module that exports a greeting function, then import and use it in your main file.
🎯 Goal: Create a module file that exports a function using module.exports, then import it in another file using require and call the function.
📋 What You'll Learn
Create a file named greet.js that exports a function called sayHello using module.exports.
The sayHello function should return the string 'Hello, Node.js!'.
Create a file named app.js that imports the sayHello function from greet.js using require.
Call the imported sayHello function in app.js.
💡 Why This Matters
🌍 Real World
Node.js uses CommonJS modules to organize code into reusable pieces. This helps keep projects clean and manageable.
💼 Career
Understanding CommonJS modules is essential for backend JavaScript development and working with many Node.js projects.
Progress0 / 4 steps
1
Create the greet.js module with sayHello function
Create a file named greet.js. Inside it, define a function called sayHello that returns the string 'Hello, Node.js!'.
Node.js
Hint
Define a function named sayHello that returns the greeting string.
2
Export the sayHello function using module.exports
In greet.js, export the sayHello function using module.exports.
Node.js
Hint
Assign the sayHello function to module.exports to export it.
3
Import the sayHello function in app.js using require
Create a file named app.js. Import the sayHello function from greet.js using require and store it in a variable called sayHello.
Node.js
Hint
Use require('./greet') to import the exported function and assign it to sayHello.
4
Call the sayHello function in app.js
In app.js, call the sayHello function.
Node.js
Hint
Simply call the sayHello() function to use it.
Practice
(1/5)
1. What does module.exports do in a Node.js file?
easy
A. It deletes the current module from memory.
B. It imports code from another module.
C. It runs the module as a standalone program.
D. It defines what the module shares when required by another file.
Solution
Step 1: Understand module.exports role
module.exports sets the object or value that other files receive when they use require() on this module.
Step 2: Differentiate from require()
require() is used to import, while module.exports is used to export code from a module.
Final Answer:
It defines what the module shares when required by another file. -> Option D
2. Which of the following is the correct syntax to import a local module named utils.js using CommonJS?
easy
A. const utils = require('./utils');
B. const utils = require('utils');
C. import utils from './utils';
D. const utils = import('./utils');
Solution
Step 1: Identify local module import syntax
Local files require a relative path starting with './' or '../' in require().
Step 2: Check each option
const utils = require('./utils'); uses require('./utils'), which correctly imports the local utils.js file. const utils = require('utils'); misses './', so it looks for a package. import utils from './utils'; uses ES module syntax, not CommonJS. const utils = import('./utils'); uses dynamic import, not CommonJS.
Final Answer:
const utils = require('./utils'); -> Option A
Quick Check:
Local modules need './' in require() [OK]
Hint: Use './' prefix for local files in require() [OK]
Common Mistakes:
Omitting './' for local modules
Using ES module import syntax in CommonJS
Using import() instead of require()
3. Given the following two files, what will be logged when node app.js runs?
// math.js
module.exports.add = (a, b) => a + b;
module.exports.sub = (a, b) => a - b;
// app.js
const math = require('./math');
console.log(math.add(5, 3));
console.log(math.sub(5, 3));
medium
A. undefined and undefined
B. 8 and 2
C. Error: add is not a function
D. 5 and 3
Solution
Step 1: Understand exports in math.js
math.js exports two functions: add and sub, which add and subtract two numbers.
Step 2: Trace app.js calls
app.js requires math.js and calls math.add(5, 3) which returns 8, and math.sub(5, 3) which returns 2.
Final Answer:
8 and 2 -> Option B
Quick Check:
5+3=8 and 5-3=2 [OK]
Hint: Check exported function names and call with correct args [OK]
Common Mistakes:
Expecting undefined because of wrong export syntax
Confusing module.exports with exports shorthand
Forgetting to require the module
4. What is the error in the following code snippet?
C. greet is not a function because exports was overwritten incorrectly.
D. No error; it logs 'Hello'.
Solution
Step 1: Analyze exports assignment in greet.js
Assigning directly to exports replaces the local exports variable but does not change module.exports, so require() gets an empty object.
Step 2: Understand require() result in app.js
Since module.exports was not changed, greet is an empty object, not a function, so calling greet() causes an error.
Final Answer:
greet is not a function because exports was overwritten incorrectly. -> Option C
Quick Check:
Overwrite exports breaks module.exports [OK]
Hint: Always assign to module.exports, not exports directly [OK]
Common Mistakes:
Assigning function directly to exports instead of module.exports
Expecting exports and module.exports to be the same after reassignment
Ignoring that require() returns module.exports
5. You want to export a single class from a module so that requiring it returns the class directly. Which is the correct way to do this in CommonJS?
class User {
constructor(name) {
this.name = name;
}
}
// What should you write here?
hard
A. module.exports = User;
B. exports.User = User;
C. module.exports.User = User;
D. export default User;
Solution
Step 1: Understand exporting a single value
To export a single class so require() returns it directly, assign it to module.exports.
Step 2: Compare options
module.exports = User; assigns User directly to module.exports, so require('./module') returns the class. module.exports.User = User; and exports.User = User; export an object with User property, so require() returns an object, not the class itself. export default User; uses ES module syntax, invalid in CommonJS.
Final Answer:
module.exports = User; -> Option A
Quick Check:
Single export = module.exports = value [OK]
Hint: Assign single export directly to module.exports [OK]
Common Mistakes:
Using exports.User instead of module.exports for single export
Mixing ES module syntax with CommonJS
Expecting require() to return class when exporting as property