Challenge - 5 Problems
Module Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why use modules in Node.js?
Which of the following best explains why modules are important in Node.js?
Attempts:
2 left
💡 Hint
Think about how breaking code into parts helps manage complexity.
✗ Incorrect
Modules let you split your code into smaller files. This helps keep code clean and reusable. It also prevents variables or functions from clashing with each other.
❓ component_behavior
intermediate2:00remaining
What happens when you import a module twice?
In Node.js, if you import the same module twice in different files, what happens?
Attempts:
2 left
💡 Hint
Think about efficiency and how Node.js caches modules.
✗ Incorrect
Node.js caches modules after the first time they are loaded. So importing the same module multiple times returns the same object without rerunning the code.
📝 Syntax
advanced2:00remaining
Identify the correct way to export a function in a Node.js module
Which option correctly exports a function named
greet from a Node.js module?Node.js
function greet() {
console.log('Hello!');
}Attempts:
2 left
💡 Hint
Remember how to assign the exported value in CommonJS modules.
✗ Incorrect
In Node.js CommonJS modules, module.exports is used to export a value. Assigning directly to exports won't work as expected.
🔧 Debug
advanced2:00remaining
Why does this module import fail?
Given this code in
app.js:
const utils = require('./utils');
console.log(utils.add(2, 3));
And this code in utils.js:
function add(a, b) {
return a + b;
}
Why does running node app.js cause an error?Attempts:
2 left
💡 Hint
Check if the function is made available outside the module.
✗ Incorrect
The add function is defined but not exported from utils.js. So require('./utils') returns an empty object.
❓ lifecycle
expert2:00remaining
When is module code executed in Node.js?
At what point does Node.js run the code inside a module file?
Attempts:
2 left
💡 Hint
Think about when Node.js loads and caches modules.
✗ Incorrect
Node.js runs the module code once when it is first imported. Later imports use the cached exports without rerunning the code.