{"record":{"id":"e013ad823d57ddd1","repo":"hashicorp/terraform","slug":"root-module-not-found-please-run-terraform-init","errorCode":null,"errorMessage":"root module not found. Please run terraform init","messagePattern":"root module not found\\. Please run terraform init","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/modules.go","lineNumber":78,"sourceCode":"\t\tloader.Parser().ForceFileSource(filename, src)\n\t})\n\tdiags = diags.Append(varDiags)\n\tif diags.HasErrors() {\n\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\n\trootModPath, err := ModulePath([]string{})\n\tif err != nil {\n\t\tdiags = diags.Append(err)\n\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\n\t// Read the root module path so we can then traverse the tree\n\trootModEarly, earlyConfDiags := c.loadSingleModule(rootModPath)\n\tif rootModEarly == nil {\n\t\tdiags = diags.Append(errors.New(\"root module not found. Please run terraform init\"), earlyConfDiags)\n\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\n\tdiags = diags.Append(c.resolveConstVariables(rootModPath, args.ViewType))\n\tif diags.HasErrors() {\n\t\tview.Diagnostics(diags)\n\t\treturn 1\n\t}\n\n\tconfig, confDiags := c.loadConfig(rootModPath)\n\t// Here we check if there are any uninstalled dependencies\n\tversionDiags := terraform.CheckCoreVersionRequirements(config)\n\tif versionDiags.HasErrors() {\n\t\tview.Diagnostics(versionDiags)\n\t\treturn 1\n\t}\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/modules.go#L60-L96","documentation":"Returned by the modules/providers inspection command (modules.go:78). loadSingleModule(rootModPath) returned a nil root module plus earlyConfDiags. This typically means the .terraform directory / module manifest is missing, so the configuration tree could not be loaded — hence the hint to run `terraform init`.","triggerScenarios":"Running `terraform providers` or `terraform modules` in a directory that has never been initialized, or whose .terraform was deleted, or where the root config failed to parse so the module loader returned nil.","commonSituations":"Fresh clone of a repo without running init first; .terraform dir removed to save space; broken module source references that make the loader bail out; running the command outside any terraform directory.","solutions":["Run `terraform init` in the directory to install modules/providers and build the module tree.","Verify you are in the directory containing the root *.tf files (ModulePath([]string{}) resolves the current dir).","Inspect earlyConfDiags shown alongside the error for parse/module-source problems and fix those, then re-init."],"exampleFix":"# before\n cd my-stack && terraform providers   # root module not found\n# after\n cd my-stack && terraform init && terraform providers","handlingStrategy":"validation","validationCode":"// Before running terraform providers/modules, ensure the dir is initialized.\n if _, err := os.Stat(\".terraform\"); os.IsNotExist(err) {\n     exec.Command(\"terraform\", \"init\").Run()\n }","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"terraform\", \"providers\").CombinedOutput()\n if err != nil && bytes.Contains(out, []byte(\"run terraform init\")) {\n     exec.Command(\"terraform\", \"init\").Run()\n     exec.Command(\"terraform\", \"providers\").Run()\n }","preventionTips":["Always run `terraform init` after a fresh clone before any inspection command.","Verify the current directory contains the root .tf files."],"tags":["init","modules","configuration","bootstrap"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}