Node.js "Cannot find module './dist'" - Build Step Not Run in CI
A script imported ./dist/... but the build that produces dist/ never ran in this job, or ran in the wrong order. The directory is absent, so Node cannot resolve the entry.
What this error means
A start, test, or pack step fails with Cannot find module './dist'. Locally the folder is left over from a previous build; on a clean CI checkout it does not exist yet.
Error: Cannot find module '/work/repo/dist/index.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
code: 'MODULE_NOT_FOUND'Common causes
The build step did not run before the consumer
The job ran node dist/index.js (or a test that imports it) without first running npm run build. On a fresh checkout there is no stale dist/ to mask the gap.
dist is gitignored and not regenerated
The output is correctly excluded from git, so CI must build it. If the workflow skips the build, the directory is simply missing.
How to fix it
Run the build before any step that needs dist
Order the job so compilation happens first, then start or test.
- run: npm ci
- run: npm run build
- run: npm testMake package scripts depend on the build
Wire a prepublish/prestart so the artifact is always built before it is consumed.
{
"scripts": {
"build": "tsc -p tsconfig.json",
"prestart": "npm run build",
"start": "node dist/index.js"
}
}How to prevent it
- Always run the build step on a clean checkout before consuming its output.
- Keep
dist/gitignored and treat it as a CI-produced artifact. - Use
prepare/prestarthooks so the build cannot be skipped.