03 / 05

What are the modules in Node.js?

Difficulty: 3/10
CommonJS, ES Modules, Built-in Modules

Modules are like JavaScript libraries that can be used in a Node.js application to include a set of functions. To include a module in a Node.js application, use the require() function or import with the parentheses containing the module's name.

Scenario Questions

0-2 years experience

  1. 1You need to read a config file at startup. How do you import the 'fs' module and use readFileSync? What if the file doesn't exist?
  2. 2A teammate wrote a helper file with 'module.exports = { parseDate }'. In your file, you write 'const { parseDate } = require('./helpers')' but get undefined. What's wrong?
  3. 3You're setting up a new Node project and see 'type': 'module' in package.json. How does that change how you write imports compared to a project without it?

2-5 years experience

  1. 1You're incrementally migrating a CommonJS codebase to ESM. A file uses 'require('./config')' where config.js uses 'module.exports = {...}'. After renaming config.js to config.mjs and changing to 'export default {...}', the import breaks. Why?
  2. 2A service you own starts throwing 'ERR_MODULE_NOT_FOUND' for a dependency that definitely exists in node_modules. Walk me through how you'd debug the module resolution.
  3. 3Your team debates using dynamic import() for a rarely-used heavy module (like a PDF generator) vs static import at top of file. What factors drive the decision?

5-8 years experience

  1. 1Design a plugin system where external packages register themselves via a 'plugins' field in package.json. How do you safely load and validate each plugin at runtime without crashing the host process?
  2. 2A large monorepo has circular dependencies between internal packages (pkg-a imports pkg-b, pkg-b imports pkg-a). How does Node's module cache handle this, and what patterns prevent runtime surprises?
  3. 3You're evaluating whether to adopt ESM across a 50-service backend. What's the migration strategy that avoids a flag-day cutover, and how do you handle transitive dependencies that haven't published ESM builds?

8+ years experience

  1. 1Your org maintains a shared internal module registry. Teams publish v1, v2, v3 of 'core-utils' with breaking changes. How do you design module resolution and versioning policy so services can upgrade independently without diamond dependency conflicts?
  2. 2You're architecting a micro-frontend platform where each team deploys a separate Node-based SSR fragment. How do you define module boundaries and shared dependencies (React, design system) to avoid version skew and duplicate downloads?
  3. 3A legacy CommonJS codebase has 200k lines and zero tests. Leadership wants ESM for top-level await and tree-shaking. Propose a multi-quarter migration plan that delivers incremental value and doesn't block feature work.

Follow-up Questions

  • How does Node resolve a require('./utils') vs require('lodash')?
  • What happens when you mix CommonJS and ESM in the same project?
  • When would you use dynamic import() over static import?
Share

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