Bird
Raised Fist0
Node.jsframework~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

      Practice

      (1/5)
      1. Which syntax is used to import modules in CommonJS in Node.js?
      easy
      A. const module = require('module')
      B. import module from 'module'
      C. module.import('module')
      D. load('module')

      Solution

      1. Step 1: Understand CommonJS import syntax

        CommonJS uses the require function to import modules.
      2. Step 2: Compare with other options

        Options A, B, and C are not valid CommonJS import syntax; B is ESM syntax.
      3. Final Answer:

        const module = require('module') -> Option A
      4. Quick Check:

        CommonJS imports use require() [OK]
      Hint: CommonJS uses require(), ESM uses import [OK]
      Common Mistakes:
      • Confusing ESM import syntax with CommonJS
      • Using import in CommonJS files
      • Trying to use module.import() which doesn't exist
      2. Which of the following is the correct way to export a function in ESM syntax?
      easy
      A. export = function() {}
      B. module.exports = function() {}
      C. exports.function = function() {}
      D. export default function() {}

      Solution

      1. Step 1: Identify ESM export syntax

        ESM uses export default to export a default function or value.
      2. Step 2: Eliminate CommonJS and invalid options

        module.exports = function() {} and C are CommonJS exports; D is invalid syntax.
      3. Final Answer:

        export default function() {} -> Option D
      4. Quick Check:

        ESM default export uses export default [OK]
      Hint: ESM exports use export default or named export [OK]
      Common Mistakes:
      • Using module.exports in ESM files
      • Trying to use export = which is invalid in ESM
      • Confusing exports.function with ESM named exports
      3. Given this CommonJS code, what will be the output?
      const message = require('./message');
      console.log(message);
      And the file message.js contains:
      module.exports = 'Hello from CommonJS';
      medium
      A. 'Hello from CommonJS'
      B. undefined
      C. SyntaxError
      D. ReferenceError

      Solution

      1. Step 1: Understand module.exports in CommonJS

        The file exports a string 'Hello from CommonJS' using module.exports.
      2. Step 2: Import and log the exported value

        The require call imports the string, so console.log prints it.
      3. Final Answer:

        'Hello from CommonJS' -> Option A
      4. Quick Check:

        CommonJS require returns module.exports value [OK]
      Hint: require() returns module.exports value [OK]
      Common Mistakes:
      • Expecting require to return an object by default
      • Confusing with ESM import behavior
      • Thinking require throws error without .js extension
      4. What is wrong with this ESM import statement in a Node.js file?
      const fs = require('fs');
      medium
      A. Missing semicolon at the end
      B. require is not defined in ESM modules
      C. fs module cannot be imported in Node.js
      D. Should use import fs from 'fs' instead

      Solution

      1. Step 1: Identify module system used

        The code uses require which is CommonJS syntax, but in ESM modules, require is not available.
      2. Step 2: Understand ESM import rules

        ESM modules must use import statements; require is undefined.
      3. Final Answer:

        require is not defined in ESM modules -> Option B
      4. Quick Check:

        require() undefined in ESM [OK]
      Hint: require() is undefined in ESM modules [OK]
      Common Mistakes:
      • Thinking require works in ESM without extra setup
      • Confusing missing semicolon as error
      • Believing fs cannot be imported in Node.js
      5. You want to write a Node.js module that can be imported both by CommonJS and ESM users without errors. Which approach is best?
      hard
      A. Use module.exports and add a separate export default for ESM
      B. Write only CommonJS syntax and use dynamic import() in ESM
      C. Use a dual package approach with conditional exports in package.json
      D. Write only ESM syntax and rename file to .mjs

      Solution

      1. Step 1: Understand compatibility challenges

        CommonJS and ESM have different import/export systems, so a single file often can't serve both seamlessly.
      2. Step 2: Use Node.js dual package support

        Node.js supports conditional exports in package.json to provide separate entry points for CommonJS and ESM consumers.
      3. Step 3: Evaluate other options

        Use module.exports and add a separate export default for ESM mixes syntaxes which doesn't work reliably, C requires dynamic import in consumers, D limits to ESM-only.
      4. Final Answer:

        Use a dual package approach with conditional exports in package.json -> Option C
      5. Quick Check:

        Dual package conditional exports solve CommonJS/ESM compatibility [OK]
      Hint: Use package.json conditional exports for dual support [OK]
      Common Mistakes:
      • Trying to mix module.exports and export default in one file
      • Using only .mjs files limits CommonJS users
      • Relying on dynamic import() without package config