Skip to the solution
MediumFrontend10 min read

How do you debug type-resolution and ESM/CJS mismatches with traceResolution and explain?

770 views

The problem

Question presented to candidate: "A teammate gets TS1479 importing an ESM-only package, and in another project the types compile but Node crashes with 'does not provide an export named default'. How would you use tsc --traceResolution and --explainFiles to find out what TypeScript actually resolved, and why?"

What a strong answer should cover:

  • --traceResolution prints every module lookup: the mode (ESM or CJS), the conditions used (import/require, types, node), each exports condition matched or failed, every file tried and the final result
  • --explainFiles prints why each file is in the program (include pattern, which import pulled it in) and, under node16/nodenext, whether each file is ESM or CommonJS and which package.json field decided it
  • Reading TS1479: the importing file is CommonJS (no "type": "module"), the target is ESM; fixes are "type": "module", a .mts file or a dynamic import()
  • Reading a fallback: when the matched condition has no declaration file, tsc tries later conditions, so it may type-check against a different file than Node loads (masquerading types)
  • Practical workflow: redirect the output to a file and grep for the module name, --listFilesOnly and --showConfig for quick checks, and the attw tool for published packages

Clarifying questions expected:

  • What is the moduleResolution setting, and is the importing package ESM or CommonJS?
  • Is the problem a compile error, or code that compiles and fails at run time?

Code / implementation expected: Optional β€” the commands, and how to read the relevant trace lines.

  • tooling
  • debugging

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 Β· 9 sections

Run the code

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

TypeScriptReading a real --traceResolution excerpt

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 33 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 does the declare keyword do?

Continue