Skip to the solution
MediumFrontend10 min read

When should you use moduleResolution: "bundler" versus "nodenext"?

498 views

The problem

Question presented to candidate: "You are setting up the tsconfig for a new project. How do you decide between moduleResolution: \"bundler\" and \"nodenext\"? What does each one let you write, and what goes wrong if you pick the wrong one?"

What a strong answer should cover:

  • Module resolution is how the compiler maps an import string to a file; the setting should model whatever will really load the code at run time
  • bundler models Vite, webpack, esbuild and similar: extensionless relative imports and directory index files resolve, and package exports are read with the import and types conditions
  • nodenext models Node.js: the importing file is ESM or CommonJS (from .mts/.cts or package.json "type"), ESM imports need the full .js extension (TS2835, TS2834), and exports use import or require accordingly
  • Pairing rules: nodenext resolution requires module: "nodenext" (TS5110 / TS5109); in 5.4 bundler needs module preserve or ES2015+ (TS5095)
  • The failure mode of choosing bundler for code Node runs directly: tsc is happy, Node throws ERR_MODULE_NOT_FOUND
  • Rule of thumb: bundled apps use bundler; code that Node (or a published library's consumers) loads uses nodenext; nodenext-valid code also resolves under bundler

Clarifying questions expected:

  • What actually runs the output: a bundler, Node directly, or both (a published library)?
  • Is the package ESM ("type": "module") or CommonJS?

Code / implementation expected: Optional β€” two small tsconfigs and an import that one accepts and the other rejects.

  • moduleResolution
  • bundler

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

10 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptA model of both resolvers on the same project

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 43 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What is a discriminated union?

Continue