0
0
Node.jsframework~5 mins

CommonJS vs ESM differences in Node.js - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is CommonJS in Node.js?
CommonJS is a module system used in Node.js that uses require() to load modules and module.exports to export them. It loads modules synchronously.
Click to reveal answer
beginner
What does ESM stand for and what is its main feature?
ESM stands for ECMAScript Modules. It is the official JavaScript module system that uses import and export syntax and supports asynchronous loading.
Click to reveal answer
intermediate
How do you export a function in CommonJS vs ESM?
In CommonJS: module.exports = functionName;
In ESM: export function functionName() {} or export default functionName;
Click to reveal answer
intermediate
Can CommonJS and ESM modules be mixed in the same Node.js project?
Yes, but with some restrictions. ESM can import CommonJS modules, but CommonJS cannot directly import ESM without dynamic import or special handling.
Click to reveal answer
beginner
What file extensions and package settings indicate ESM usage in Node.js?
Files with .mjs extension or .js files with "type": "module" in package.json are treated as ESM modules.
Click to reveal answer
Which syntax is used to import modules in ESM?
Aexport default moduleName;
Bimport moduleName from 'module';
Cmodule.exports = moduleName;
Dconst moduleName = require('module');
Which module system loads modules synchronously in Node.js?
ACommonJS
BBoth ESM and CommonJS
CESM
DNeither
How do you mark a Node.js project to use ESM for .js files?
AAdd "type": "module" in package.json
BRename files to .cjs
CUse require() instead of import
DAdd "type": "commonjs" in package.json
Which of these is NOT true about CommonJS and ESM?
ACommonJS uses module.exports to export
BESM uses export keyword
CCommonJS supports top-level await
DESM supports asynchronous loading
Can CommonJS modules import ESM modules directly using require()?
AOnly in older Node.js versions
BYes, always
COnly if using .mjs extension
DNo, not directly
Explain the main differences between CommonJS and ESM module systems in Node.js.
Think about how modules are loaded and how you write import/export code.
You got /5 concepts.
    Describe how you can use both CommonJS and ESM modules together in a Node.js project.
    Consider interoperability and loading methods.
    You got /4 concepts.