Modules (CommonJS & ESM) Questions with Answers

17 practice questions on Modules (CommonJS & ESM) (Node.js & Backend APIs) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

Which package.json setting makes Node treat the project's .js files as ES modules?

  1. "module": "esm"
  2. "type": "module"
  3. "esm": true
  4. "main": "module"

Answer: B. "type": "module"

Explanation

With "type": "module" the .js files in that package scope are loaded as ESM; without it (or with "commonjs") they are CommonJS.

Step-by-step solution

  1. Setting "type": "module" tells Node to use ES modules.
  2. This changes .js files to be treated as ESM by default.
  3. Other options like "module": "esm" are not valid syntax for this purpose.
  4. The correct setting is "type": "module".
  5. Final answer: "type": "module".

Why the other options are wrong

  • A. Setting module to esm is not a valid package.json option for ES module mode.
  • C. An esm flag in package.json is not a Node setting; the type field is what switches ES module mode.
  • D. A main field points to the entry file; setting it to module is not how to enable ES modules.

Tip: Use "type": "module" in package.json to enable ESM.

Question 2Easy

Where can await be used at the top level of a file, outside any async function?

  1. In ES modules
  2. In CommonJS modules
  3. Only inside the Node REPL
  4. Nowhere; it is always a syntax error

Answer: A. In ES modules

Explanation

Top-level await is an ESM feature. In a CommonJS file you must wrap the code in an async function.

Step-by-step solution

  1. Top-level await is allowed only in ES modules (.mjs files or type module packages).
  2. CommonJS files are wrapped in a function, so await there is a syntax error.
  3. The correct answer is option A: In ES modules.

Why the other options are wrong

  • B. CommonJS has no top-level await; it is only valid inside async functions.
  • C. Top-level await also works in the REPL, so the word only is wrong; files that are ES modules can use it anywhere.
  • D. It is valid in ES modules, so it is not always a syntax error.

Tip: Top-level await means an ES module. If a file uses require or module.exports, it cannot use top-level await.

Question 3Easy

How does require('fs') differ from require('./fs')?

  1. 'fs' is the built-in module; './fs' is a local file
  2. Both load the same built-in fs module
  3. './fs' loads the built-in, 'fs' a local file
  4. 'fs' searches only node_modules for a package

Answer: A. 'fs' is the built-in module; './fs' is a local file

Explanation

A bare name is resolved as a core module or package; a specifier starting with ./ or ../ is a path relative to the current file.

Step-by-step solution

  1. require('fs') loads the built-in fs module.
  2. require('./fs') looks for a local file named fs in the current directory.
  3. Core modules are loaded directly without path resolution.
  4. ./fs is treated as a relative path to a local file.
  5. The difference lies in whether it's a core module or a local file.
  6. Final answer: 'fs' is the built-in module; './fs' is a local file.

Why the other options are wrong

  • B. The two forms do not load the same module; the built-in fs and a local fs file are different.
  • C. The ./ prefix marks a local file, so './fs' cannot load the built-in module.
  • D. A bare name like fs is not limited to node_modules; core modules are checked first, before any package.

Tip: Remember: bare names are core modules, paths are local files.

Question 4Easy

What does the node: prefix in import fs from 'node:fs' signal?

  1. The module is downloaded from the npm registry when first imported
  2. The module should load in a worker thread
  3. The import is evaluated lazily on first use
  4. The specifier always refers to the Node built-in module

Answer: D. The specifier always refers to the Node built-in module

Explanation

node: makes it explicit that a core module is meant, so a package named fs in node_modules can never shadow it.

Step-by-step solution

  1. Node: prefix tells Node.js the specifier refers to a built-in module.
  2. This ensures the fs module is not replaced by a package named fs in node_modules.
  3. The import syntax explicitly uses the built-in module instead of a local one.
  4. Built-in modules are part of Node.js core and always available.
  5. So the specifier always refers to the Node built-in module.
  6. Final answer: The specifier always refers to the Node built-in module.

Why the other options are wrong

  • A. The node: prefix does not download anything; it only marks that the name is a built-in module.
  • B. The node: prefix has nothing to do with worker threads; it only selects a built-in module.
  • C. Imports are not lazy by default, and the node: prefix does not change when the module runs.

Tip: Use node: for built-in modules to prevent naming clashes with local packages.

Question 5Medium

Which statement about ESM import declarations is true?

  1. They run in order, each one only when execution reaches that line
  2. They may appear inside if blocks
  3. They are hoisted and resolved before the module body runs
  4. They are evaluated lazily on first use

Answer: C. They are hoisted and resolved before the module body runs

Explanation

Static imports are linked before evaluation, so the dependency graph is known up front. Use dynamic import() for conditional loading.

Step-by-step solution

  1. ESM import declarations are processed before the module code runs.
  2. This means dependencies are resolved early in the execution process.
  3. Hoisting ensures all imports are available when the module body starts.
  4. Import declarations are not lazy; they are resolved upfront.
  5. So, the correct statement is that they are hoisted and resolved before the module body runs.

Why the other options are wrong

  • A. Imports are not run line by line; they are resolved before the module body starts.
  • B. Import declarations must be at the top level and cannot sit inside an if block.
  • D. Import declarations are not lazy; they are resolved up front, unlike a dynamic import().

Tip: Remember: ESM imports are hoisted and resolved upfront, not lazily.

Question 6Medium

What does running node main.js print?

// counter.js
let count = 0;
module.exports = () => ++count;
console.log('loaded');
// main.js
const a = require('./counter');
const b = require('./counter');
console.log(a(), b(), a === b);
  1. loaded 1 2 true
  2. loaded loaded 1 1 false
  3. loaded 1 1 false
  4. loaded loaded 1 2 true

Answer: A. loaded 1 2 true

Explanation

Modules are cached after the first require: the file runs once, both variables get the same function and the counter is shared.

Step-by-step solution

  1. Require caches the module, so counter.js runs once and prints 'loaded' once.
  2. A and b are the same function from the cache, so a === b is true.
  3. a() returns 1, then b() returns 2, because they share the count.
  4. Output: loaded 1 2 true, option A.

Why the other options are wrong

  • B. The module runs only once, and a and b are the same function, so the answer is not false.
  • C. The module runs once, so loaded prints once, and a === b is true.
  • D. Loaded is printed once, not twice, because require caches the module.

Tip: Require caches modules: code runs once, and every require returns the same object.

Question 7Medium

What does main.js print?

// math.js
exports = { add: (a, b) => a + b };
// main.js
const m = require('./math');
console.log(typeof m.add);
  1. function
  2. object
  3. It throws a TypeError
  4. undefined

Answer: D. undefined

Explanation

Assigning to exports only rebinds the local variable; module.exports is still the original empty object, so m.add is undefined.

Step-by-step solution

  1. The code assigns a new object to exports, which only replaces the local variable exports.
  2. module.exports is still the original empty object {}.
  3. m.add is undefined, so typeof prints undefined, option D.

Why the other options are wrong

  • A. Add is never attached to the exported object, so it is not a function.
  • B. The module exports {} but the property add is missing, so m.add is undefined, not an object.
  • C. No error is thrown; typeof on a missing property is safe.

Tip: To export, set module.exports = ... or add properties to exports. Reassigning exports breaks the link.

Question 8Medium

What does this CommonJS file print?

console.log(this === module.exports);
  1. false
  2. undefined
  3. true
  4. It throws a ReferenceError

Answer: C. true

Explanation

CommonJS wraps each file in a function that is called with this set to module.exports. In an ES module the top-level this is undefined.

Step-by-step solution

  1. CommonJS wraps files in a function.
  2. this equals module.exports.
  3. The output is true.

Why the other options are wrong

  • A. It's true.
  • B. It prints a boolean.
  • D. No error.

Tip: this = exports.

Question 9Hard

What does node a.js print?

// a.js
exports.done = false;
const b = require('./b');
console.log('in a, b.done =', b.done);
exports.done = true;
// b.js
const a = require('./a');
console.log('in b, a.done =', a.done);
exports.done = true;
  1. in b, a.done = true in a, b.done = true
  2. in b, a.done = false in a, b.done = true
  3. in a, b.done = true in b, a.done = false
  4. It throws a circular dependency error

Answer: B. in b, a.done = false in a, b.done = true

Explanation

With a cycle, require('./a') inside b.js returns a's exports as they are at that moment (partially filled, done = false). b then finishes and a continues.

Step-by-step solution

  1. a.js sets exports.done to false, then requires b.js.
  2. b.js requires a.js and gets the partly filled exports, where done is still false.
  3. b.js prints 'in b, a.done = false', sets its done to true and returns.
  4. Back in a.js, b.done is true, so it prints 'in a, b.done = true'. The correct answer is B (in b, a.done = false in a, b.done = true).

Why the other options are wrong

  • A. a.done is false when b.js runs, because a.js sets done to true only after the require returns.
  • C. The lines come in the wrong order; b.js runs during the require, so its line prints first.
  • D. Node does not throw on circular requires; it returns the partial exports object.

Tip: In a circular require, the module that is still loading gives back its partial exports. Changes made after the require are not seen yet.

Question 10Hard

What does node main.mjs print?

// counter.mjs
export let count = 0;
export function inc() { count++; }
// main.mjs
import { count, inc } from './counter.mjs';
inc();
console.log(count);
  1. 0
  2. 1
  3. undefined
  4. It throws: imports are read-only

Answer: B. 1

Explanation

ESM exports are live bindings: the imported count reflects the exporter's variable, which inc() changed to 1. Assigning to count in main.mjs would throw, but reading is fine.

Step-by-step solution

  1. Import { count, inc } from './counter.mjs' imports the live binding of count.
  2. inc() increments the exported count variable to 1.
  3. console.log(count) reads the updated value of count, which is now 1.
  4. Reading a live binding does not throw an error.
  5. The final printed value is 1.

Why the other options are wrong

  • A. The counter is incremented to 1 by inc(), so the output is not 0.
  • C. count is a defined export with the value 0, so it is never undefined.
  • D. Named imports are live bindings and can be read; nothing in this code throws an error.

Tip: In ESM, imported variables are live bindings. Reading them after changes shows the updated value.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Node.js & Backend APIs topics · All topics