hashicorp/terraform · error

root module not found. Please run terraform init

Error message

root module not found. Please run terraform init

What it means

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`.

Source

Thrown at internal/command/modules.go:78

		loader.Parser().ForceFileSource(filename, src)
	})
	diags = diags.Append(varDiags)
	if diags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

	rootModPath, err := ModulePath([]string{})
	if err != nil {
		diags = diags.Append(err)
		view.Diagnostics(diags)
		return 1
	}

	// Read the root module path so we can then traverse the tree
	rootModEarly, earlyConfDiags := c.loadSingleModule(rootModPath)
	if rootModEarly == nil {
		diags = diags.Append(errors.New("root module not found. Please run terraform init"), earlyConfDiags)
		view.Diagnostics(diags)
		return 1
	}

	diags = diags.Append(c.resolveConstVariables(rootModPath, args.ViewType))
	if diags.HasErrors() {
		view.Diagnostics(diags)
		return 1
	}

	config, confDiags := c.loadConfig(rootModPath)
	// Here we check if there are any uninstalled dependencies
	versionDiags := terraform.CheckCoreVersionRequirements(config)
	if versionDiags.HasErrors() {
		view.Diagnostics(versionDiags)
		return 1
	}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Run `terraform init` in the directory to install modules/providers and build the module tree.
  2. Verify you are in the directory containing the root *.tf files (ModulePath([]string{}) resolves the current dir).
  3. Inspect earlyConfDiags shown alongside the error for parse/module-source problems and fix those, then re-init.

Example fix

# before
 cd my-stack && terraform providers   # root module not found
# after
 cd my-stack && terraform init && terraform providers
Defensive patterns

Strategy: validation

Validate before calling

// Before running terraform providers/modules, ensure the dir is initialized.
 if _, err := os.Stat(".terraform"); os.IsNotExist(err) {
     exec.Command("terraform", "init").Run()
 }

Try / catch

out, err := exec.Command("terraform", "providers").CombinedOutput()
 if err != nil && bytes.Contains(out, []byte("run terraform init")) {
     exec.Command("terraform", "init").Run()
     exec.Command("terraform", "providers").Run()
 }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/e013ad823d57ddd1. Report an issue: GitHub.