Challenge - 5 Problems
CommonJS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this CommonJS module import?
Consider two files:
math.js:
app.js:
What will be printed when running
math.js:
module.exports = { add: (a, b) => a + b };app.js:
const math = require('./math');
console.log(math.add(2, 3));What will be printed when running
node app.js?Node.js
module.exports = { add: (a, b) => a + b };
const math = require('./math');
console.log(math.add(2, 3));Attempts:
2 left
💡 Hint
Remember that module.exports defines what is returned by require.
✗ Incorrect
The math.js exports an object with an add function. Requiring it returns that object, so math.add(2, 3) returns 5.
❓ Predict Output
intermediate2:00remaining
What error does this code raise?
Given the file greet.js:
And app.js:
What happens when running
exports = function() { return 'Hello'; };And app.js:
const greet = require('./greet');
console.log(greet());What happens when running
node app.js?Node.js
exports = function() { return 'Hello'; };
const greet = require('./greet');
console.log(greet());Attempts:
2 left
💡 Hint
Assigning to exports alone does not change module.exports.
✗ Incorrect
Assigning directly to exports breaks the link to module.exports, so require returns an empty object. Calling greet() causes a TypeError.
❓ component_behavior
advanced2:00remaining
How does module caching affect this code?
Consider counter.js:
And app.js:
What is the output when running
let count = 0; module.exports.increment = () => ++count; module.exports.get = () => count;
And app.js:
const c1 = require('./counter');
const c2 = require('./counter');
c1.increment();
c2.increment();
console.log(c1.get());What is the output when running
node app.js?Node.js
let count = 0; module.exports.increment = () => ++count; module.exports.get = () => count; const c1 = require('./counter'); const c2 = require('./counter'); c1.increment(); c2.increment(); console.log(c1.get());
Attempts:
2 left
💡 Hint
Modules are cached and shared across requires.
✗ Incorrect
Both c1 and c2 reference the same cached module instance, so increments accumulate. After two increments, count is 2.
📝 Syntax
advanced2:00remaining
Which option correctly exports a single function?
You want to export a single function from
logger.js so that requiring it returns that function directly. Which code does this correctly?Attempts:
2 left
💡 Hint
To export a single function, assign it to module.exports directly.
✗ Incorrect
Assigning a function directly to module.exports makes require return that function. Other options export an object or break the export.
🔧 Debug
expert3:00remaining
Why does this module export fail to work as expected?
Given data.js:
And app.js:
What is the output and why?
const data = { value: 42 };
exports = data;And app.js:
const imported = require('./data');
console.log(imported.value);What is the output and why?
Node.js
const data = { value: 42 };
exports = data;
const imported = require('./data');
console.log(imported.value);Attempts:
2 left
💡 Hint
Remember the difference between exports and module.exports references.
✗ Incorrect
Reassigning exports breaks the link to module.exports, so require returns the original empty object, making imported.value undefined.