cdktf init fails with "ERROR: Cannot initialize a project in a non-empty directory"
Explains that cdktf init only scaffolds new projects and refuses non-empty directories, and what to run instead (cdktf get for providers, or init in an empty dir). Use when cdktf init errors on a non-empty directory. Not for synth or deploy failures.
TL;DR
cdktf init is only for creating new projects; it will not run in a directory that already has files. Run it in an empty directory, or if the project already exists, use cdktf get to fetch providers instead.
The error
➜ cdktf init
ERROR: Cannot initialize a project in a non-empty directoryFix it
- If you are starting fresh:
mkdir my-project && cd my-project && cdktf init --template=typescript. - If the project already exists (you have
main.ts/cdktf.json): do NOT re-init; runcdktf getto generate provider bindings (the cdktf equivalent ofterraform initfor providers). - Quick mapping:
cdktf get~terraform init(fetch providers),cdktf synth~terraform plan(generate config),cdktf deploy~terraform apply.
Expected result: providers generate into .gen (or imports) without touching your existing code.
When to use this
cdktf initrefuses with the non-empty-directory error- You want provider bindings in an existing project
When NOT to use this
- You actually want a new project (use an empty directory)
cdktf getitself fails (different problem)
Root cause
cdktf init scaffolds template files (cdktf.json, main.ts, package.json) and refuses to run where files exist to avoid overwriting your work. Provider fetching is a separate command (cdktf get) precisely so existing projects can refresh bindings safely.
Edge cases
- Hidden files (like
.git) count: init refuses even if only dotfiles exist. Initialize beforegit init, or use a fresh directory.