02 / 05

Difference between require and import.

Difficulty: 5/10
module loading, ESM vs CJS, interop

The require()` and import statements serve similar purposes but have some key differences:

require():
  1. 1

    CommonJS Syntax: require('module')

  2. 2

    Synchronous: It loads modules one after another, blocking execution until the module is loaded.

  3. 3

    Supports Both CommonJS and ES Modules: You can use it to import both CommonJS and ES modules.

  4. 4

    No Explicit Default Export Support: It doesn't explicitly support default exports.

  5. 5

    Named Exports via Destructuring: You can use destructuring to import named exports.

  6. 6

    No Static Analysis (Tree Shaking): It doesn't enable tree shaking.

  7. 7

    No Dynamic Importing: It doesn't support dynamic importing.

  8. 8

    Used Primarily in Node.js.

import:
  1. 1

    ES6 Syntax: import module from 'module'

  2. 2

    Asynchronous (Lazy Loading): It loads modules only when needed, improving performance.

  3. 3

    Supports Only ES Modules: Requires .mjs extension for ES modules.

  4. 4

    Explicit Default Export Support: You can directly import default exports.

  5. 5

    Named Exports Directly: You can import named exports directly.

  6. 6

    Static Analysis: Enables tree shaking.

  7. 7

    Dynamic Importing Supported: You can use the import() function.

  8. 8

    Used in Modern JavaScript (Browser and Node.js).

Scenario Questions

0-2 years experience

  1. 1You need to add a new utility function to an existing Node.js project that uses CommonJS. How would you import that function using require, and what would you need to change if the project switched to ES modules?
  2. 2If you write `import fs from 'fs'` in a file that is currently executed with Node's default CommonJS mode, what error do you see and why?
  3. 3When you see `module.exports = { foo }` in a file, how would you import `foo` using both require and import syntax?

2-5 years experience

  1. 1Your team upgraded Node to version 14 and started seeing that some of our internal libraries that were written with `module.exports` no longer work when imported with `import`. Walk me through why this happens and how you would fix it.
  2. 2During a code review you notice a mix of `require` and `import` statements in the same file, and the build is failing. Explain the root cause and how you would resolve the issue.
  3. 3We have a feature flag that conditionally loads a heavy module only when needed. Show how you would implement lazy loading with both require and dynamic import, and discuss the trade‑offs.

5-8 years experience

  1. 1Our microservice loads configuration files at startup. We want to move from CommonJS to ES modules to enable tree‑shaking, but we also need to support runtime `require` of plugins. How would you design the module loading layer to handle both patterns without breaking existing plugins?
  2. 2A performance regression appeared after we switched a core utility from `require` to `import`. Explain possible reasons related to module caching and initialization order, and how you would investigate and mitigate.
  3. 3We need to expose a library that can be consumed by both CommonJS and ESM consumers. Describe the packaging strategy (e.g., dual package, .cjs/.mjs) and the pitfalls you must watch for.

8+ years experience

  1. 1Our organization is planning a phased migration of a large monorepo from CommonJS to ES modules over two years. Outline the high‑level migration plan, including versioning, CI checks, and how you would handle cross‑team dependencies that still use `require`.
  2. 2Several legacy services import a shared utility using `require`, but new services are built with ESM and use `import`. Discuss the long‑term maintenance implications and propose an architectural approach to unify module consumption while minimizing disruption.
  3. 3Explain how the Node.js module resolution algorithm differs between `require` and `import` when dealing with package.json `exports` and `type` fields, and how that influences our decision to adopt a single module system across all products.

Follow-up Questions

  • What happens if you try to use `import` in a file that Node treats as CommonJS?
  • Can you describe how Node caches modules differently between the two systems?
  • How would you expose a library so both `require` and `import` callers can consume it without duplication?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.