0
0
Node.jsframework~20 mins

CommonJS vs ESM differences in Node.js - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does CommonJS handle module exports compared to ESM?
Which statement correctly describes how CommonJS and ESM export modules in Node.js?
ACommonJS uses module.exports to export a single object, while ESM uses export statements to export multiple bindings.
BCommonJS uses export statements like ESM but requires a special flag to work.
CESM uses module.exports to export a single object, while CommonJS uses export statements.
DBoth CommonJS and ESM use the same syntax: export default and export named.
Attempts:
2 left
💡 Hint
Think about how you write exports in a CommonJS file versus an ESM file.
component_behavior
intermediate
2: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);
A"string" because CommonJS exports are converted to strings in ESM.
B"function" because CommonJS exports are always functions in ESM.
C"undefined" because ESM cannot import CommonJS modules.
D"object" because the entire CommonJS export is wrapped as the default export in ESM.
Attempts:
2 left
💡 Hint
Think about how ESM treats the CommonJS module.exports object when imported.
📝 Syntax
advanced
2: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');
ABoth import and require can be used without errors in the same file.
BUsing import fs from 'fs' in a CommonJS file causes a syntax error.
CUsing require('path') in an ESM file causes a syntax error.
DUsing import fs from 'fs' requires a .cjs file extension.
Attempts:
2 left
💡 Hint
Consider the file type and module system when mixing import and require.
🔧 Debug
advanced
2: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());
ABecause the CommonJS module does not export greet as a default export.
BBecause CommonJS exports are treated as a default export in ESM, named imports like { greet } are undefined.
CBecause ESM cannot import CommonJS modules at all.
DBecause the import path is missing the file extension.
Attempts:
2 left
💡 Hint
Think about how named imports work with CommonJS default exports.
state_output
expert
2: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?
AThrows a TypeError because named imports from CommonJS are undefined.
B0 and 0 printed on separate lines.
C1 and 1 printed on separate lines.
DThrows a SyntaxError due to mixing module types.
Attempts:
2 left
💡 Hint
Recall how named imports work with CommonJS default exports in ESM.