Challenge - 5 Problems
Node.js Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
How does CommonJS handle module exports compared to ESM?
Which statement correctly describes how CommonJS and ESM export modules in Node.js?
Attempts:
2 left
💡 Hint
Think about how you write exports in a CommonJS file versus an ESM file.
✗ Incorrect
CommonJS modules export a single object using module.exports. ESM allows multiple named exports and a default export using export syntax.
❓ component_behavior
intermediate2:00remaining
What happens when importing a CommonJS module in ESM?
Given an ESM file importing a CommonJS module, what is the shape of the imported object?
Node.js
import pkg from './commonjsModule.js'; console.log(typeof pkg);
Attempts:
2 left
💡 Hint
Think about how ESM treats the CommonJS module.exports object when imported.
✗ Incorrect
When importing a CommonJS module in ESM, the entire module.exports object is available as the default export, so its type is "object".
📝 Syntax
advanced2:00remaining
Identify the syntax error in mixing CommonJS and ESM imports
Which option contains a syntax error when mixing CommonJS require and ESM import in the same file?
Node.js
import fs from 'fs'; const path = require('path');
Attempts:
2 left
💡 Hint
Consider the file type and module system when mixing import and require.
✗ Incorrect
In an ESM file, using require() causes a syntax error because require is not defined. You must use import statements only.
🔧 Debug
advanced2:00remaining
Why does this ESM import fail when importing a CommonJS module?
Given this ESM code importing a CommonJS module, why does accessing a named export fail?
commonjsModule.js:
module.exports = { greet: () => 'hi' };
esmFile.mjs:
import { greet } from './commonjsModule.js';
console.log(greet());
Attempts:
2 left
💡 Hint
Think about how named imports work with CommonJS default exports.
✗ Incorrect
CommonJS modules export a single object as default in ESM. Named imports expect named exports, so { greet } is undefined.
❓ state_output
expert2:00remaining
What is the output of this mixed CommonJS and ESM code?
Consider these two files:
commonjsModule.cjs:
let count = 0;
module.exports.increment = () => ++count;
module.exports.getCount = () => count;
esmFile.mjs:
import { increment, getCount } from './commonjsModule.cjs';
console.log(increment());
console.log(getCount());
What will be the output when running esmFile.mjs with Node.js configured for ESM?
Attempts:
2 left
💡 Hint
Recall how named imports work with CommonJS default exports in ESM.
✗ Incorrect
Named imports from a CommonJS module fail because the entire export is treated as a default export object. So increment and getCount are undefined, causing a TypeError.