{"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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/modules.go#L60-L96","documentation":"Returned by the `terraform modules` command when `loadSingleModule(rootModPath)` returns a nil root module. The most common cause is that the working directory's module tree has not been installed/initialized, so the root module artifact that Terraform expects is absent.","triggerScenarios":"Running `terraform modules` (or a command that calls `loadSingleModule`) in a directory where `.terraform/modules/` and the installed module tree are missing — i.e. `terraform init` was never run, or ran with `-lock=false`/network errors that left modules uninstalled.","commonSituations":"Fresh clone where someone runs `terraform modules` before `terraform init`; CI job that skips init; module installation failed partway and left an incomplete `.terraform`.","solutions":["Run `terraform init` in the working directory to install modules, then re-run `terraform modules`.","If init fails, resolve the module source errors in the `module` blocks and re-init.","Run the command from the directory that actually contains the root module (check `-chdir` / working dir)."],"exampleFix":"# before\nterraform modules   # in a fresh dir -> error\n# after\nterraform init && terraform modules","handlingStrategy":"validation","validationCode":"// Equivalent precondition: ensure the module tree is installed before listing.\nif _, err := c.loadSingleModule(rootModPath); err != nil || rootModEarly == nil {\n    return errors.New(\"run `terraform init` first\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run `terraform init` before `terraform modules` in CI.","Fail the pipeline if `.terraform/modules/modules.json` is absent.","Run commands with the correct working directory / `-chdir`."],"tags":["modules","init-required","filesystem","terraform-cli"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}