Interview Q&A / Node.js

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How can CommonJS code load an ES module?

SHORT ANSWER

Use dynamic import(), which works in CommonJS and returns a promise. Plain require of an ES module was not allowed historically; newer Node versions added limited support, so dynamic import is the safe choice.

Explain it simply

Think of an old railway line trying to connect to a new metro line. They run on different tracks, so you need a transfer point. In Node, that transfer point is import() written as a function call. It is allowed inside CommonJS files, and it returns a promise that resolves to the module's exports.

Static import ... from lines are not allowed in a CommonJS file, and ESM-only packages (some popular ones have gone ESM-only) will fail with ERR_REQUIRE_ESM if you try require. Recent Node releases have added the ability to require an ES module in some cases, particularly when it has no top-level await, but the behaviour depends on your Node version, so check before relying on it.

Example

js
// legacy.cjs
async function main() {
  const { default: chalk } = await import("chalk");
  console.log(chalk.green("Server started"));
}
main();

The default export arrives as the default property of the module namespace object. Named exports arrive as properties with their own names.

The opposite direction is easy: an ES module can import a CommonJS file, and module.exports becomes the default import.

What to say in the interview

"CommonJS can use dynamic import(), which returns a promise. ESM can import CJS directly. Because import() is async, I either wrap it in an async function or migrate the project to ESM."

Common mistakes

  • Forgetting .default when reading a default export from import().
  • Using the result before the promise has resolved.

#nodejs#esm#commonjs

Related questions

Practise more Node.js questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Node.js quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.