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
bundlermodels Vite, webpack, esbuild and similar: extensionless relative imports and directoryindexfiles resolve, and packageexportsare read with theimportandtypesconditionsnodenextmodels Node.js: the importing file is ESM or CommonJS (from.mts/.ctsorpackage.json"type"), ESM imports need the full.jsextension (TS2835, TS2834), andexportsuseimportorrequireaccordingly- Pairing rules:
nodenextresolution requiresmodule: "nodenext"(TS5110 / TS5109); in 5.4bundlerneedsmodulepreserveor ES2015+ (TS5095) - The failure mode of choosing
bundlerfor code Node runs directly: tsc is happy, Node throwsERR_MODULE_NOT_FOUND - Rule of thumb: bundled apps use
bundler; code that Node (or a published library's consumers) loads usesnodenext;nodenext-valid code also resolves underbundler
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.