0
0
Node.jsframework~20 mins

CommonJS require and module.exports in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CommonJS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this CommonJS module import?
Consider two files:
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));
Aundefined
B5
CTypeError: math.add is not a function
DNaN
Attempts:
2 left
💡 Hint
Remember that module.exports defines what is returned by require.
Predict Output
intermediate
2:00remaining
What error does this code raise?
Given the file greet.js:
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());
ATypeError: greet is not a function
BHello
CSyntaxError
Dundefined
Attempts:
2 left
💡 Hint
Assigning to exports alone does not change module.exports.
component_behavior
advanced
2:00remaining
How does module caching affect this code?
Consider counter.js:
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());
A2
BTypeError
C0
D1
Attempts:
2 left
💡 Hint
Modules are cached and shared across requires.
📝 Syntax
advanced
2: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?
Aexports = function(msg) { console.log(msg); };
Bexports.log = function(msg) { console.log(msg); };
Cmodule.exports.log = function(msg) { console.log(msg); };
Dmodule.exports = function(msg) { console.log(msg); };
Attempts:
2 left
💡 Hint
To export a single function, assign it to module.exports directly.
🔧 Debug
expert
3:00remaining
Why does this module export fail to work as expected?
Given data.js:
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);
ATypeError because imported is not an object
B42 because exports points to data object
Cundefined because exports reassignment does not change module.exports
DSyntaxError due to invalid exports assignment
Attempts:
2 left
💡 Hint
Remember the difference between exports and module.exports references.