cdktf synth fails with "Cannot find module" and "Please verify that the package.json has a valid main entry"
Fixes the cdktf synth MODULE_NOT_FOUND crash where node cannot resolve the cdktf package (bad install or mismatched node_modules). Covers reinstalling node_modules, verifying the cdktf install, and checking the app command. Use when synth fails with Cannot find module pointing at cdktf/lib/index.js. Not for missing provider bindings.
TL;DR
Reinstall your dependencies: rm -rf node_modules package-lock.json && npm install, then re-run cdktf synth. The cdktf npm package itself is broken or partially installed.
The error
ERROR: cdktf encountered an error while synthesizing
Synth command: npx ts-node main.ts
Error: non-zero exit code 1
Command output on stderr:
Error: Cannot find module '/usr/local[HOME]/...'. Please verify that the package.json has a valid "main" entry
...
code: 'MODULE_NOT_FOUND',Fix it
- Delete the broken install:
rm -rf node_modules package-lock.json. - Reinstall cleanly:
npm install. - Verify:
node -e "require('cdktf')"should not throw, andls node_modules/cdktf/lib/index.jsshould exist. - Re-run
cdktf synth.
Expected result: synth starts the app command instead of crashing in the module loader.
When to use this
cdktf synthfails withCannot find module ... cdktf/lib/index.js/MODULE_NOT_FOUND- The stack trace goes through node module resolution, not your code
When NOT to use this
- The missing module is a provider binding (
./.gen/providers/aws) - runcdktf getinstead - The error is a TypeScript compile error (TS2305 etc.)
Root cause
The installed cdktf package is incomplete or corrupted (interrupted install, partial nodemodules copy, or a version mismatch between the global cdktf-cli and the local cdktf library). Node resolves the package.json main entry to lib/index.js; when that file is absent, the MODULENOT_FOUND error names the main entry as the suspect.
Edge cases
- Global vs local mismatch:
cdktf-cliglobally installed at one version with a localcdktfat another can confuse resolution; keep them in sync. - In Docker builds, make sure
npm installruns afterCOPY package*.jsonand that no.dockerignoreexcludeslib.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.