Report #21299
[bug\_fix] TS2307: Cannot find module './utils.js' or its corresponding type declarations
Enable \`bundler\`, \`node16\`, or \`nodenext\` moduleResolution in \`tsconfig.json\`, ensure \`package.json\` has \`"type": "module"\`, and use \`.js\` extensions on all relative imports \(even for \`.ts\` files\), or set \`allowImportingTsExtensions: true\` if using TypeScript 5.0\+ with \`bundler\` resolution.
Journey Context:
You set up a new Node.js project with TypeScript, ESM \(\`"type": "module"\`\), and ts-node. You write \`import \{ foo \} from './foo.ts'\` but get TS2307. You change it to \`./foo.js\` \(the compiled output name\) but TypeScript still complains it can't find the declaration. You try \`moduleResolution: "node"\` which is the default, but that expects CommonJS. You read the TypeScript 4.7 release notes and learn about \`moduleResolution: "node16"\`. This mode understands that \`.js\` extensions in ESM map to \`.ts\` source files during type checking. The fix works because Node16 resolution enforces the ESM specification requirement that imports must include extensions, while TypeScript's compiler uses this setting to resolve \`.js\` imports to \`.ts\` source files without needing \`.ts\` extensions in the code, matching Node.js runtime behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T14:09:42.457191+00:00— report_created — created