{"record":{"id":"407e895b815fa785","repo":"golang/go","slug":"error-reading-s-missing-module-declaration-to-s","errorCode":null,"errorMessage":"error reading %s: missing module declaration. To specify the module path:\n\tgo mod edit -module=example.com/mod","messagePattern":"error reading (.+?): missing module declaration\\. To specify the module path:\n\tgo mod edit -module=example\\.com/mod","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/modfile.go","lineNumber":70,"sourceCode":"\t\t\t\t\ttoolchain = f.Toolchain.Name\n\t\t\t\t}\n\t\t\t\treturn nil, nil, &gover.TooNewError{What: base.ShortPath(gomod), GoVersion: f.Go.Version, Toolchain: toolchain}\n\t\t\t}\n\t\t}\n\n\t\t// Errors returned by modfile.Parse begin with file:line.\n\t\treturn nil, nil, fmt.Errorf(\"errors parsing %s:\\n%w\", base.ShortPath(gomod), shortPathErrorList(err))\n\t}\n\tif f.Go != nil && gover.Compare(f.Go.Version, gover.Local()) > 0 {\n\t\ttoolchain := \"\"\n\t\tif f.Toolchain != nil {\n\t\t\ttoolchain = f.Toolchain.Name\n\t\t}\n\t\treturn nil, nil, &gover.TooNewError{What: base.ShortPath(gomod), GoVersion: f.Go.Version, Toolchain: toolchain}\n\t}\n\tif f.Module == nil {\n\t\t// No module declaration. Must add module path.\n\t\treturn nil, nil, fmt.Errorf(\"error reading %s: missing module declaration. To specify the module path:\\n\\tgo mod edit -module=example.com/mod\", base.ShortPath(gomod))\n\t} else if err := CheckReservedModulePath(f.Module.Mod.Path); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"error reading %s: invalid module path: %q\", base.ShortPath(gomod), f.Module.Mod.Path)\n\t}\n\n\treturn data, f, err\n}\n\nfunc shortPathErrorList(err error) error {\n\tif el, ok := errors.AsType[modfile.ErrorList](err); ok {\n\t\tfor i := range el {\n\t\t\tel[i].Filename = base.ShortPath(el[i].Filename)\n\t\t}\n\t}\n\treturn err\n}\n\n// A modFileIndex is an index of data corresponding to a modFile\n// at a specific point in time.","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/modfile.go#L52-L88","documentation":"Returned by modload.ReadModFile when the parsed go.mod has no 'module' directive at all. The go.mod file is structurally parseable but is missing the mandatory module path declaration, so the toolchain cannot identify the module.","triggerScenarios":"Any 'go' command reading a go.mod that lacks a 'module <path>' line — typically a freshly created or accidentally truncated go.mod.","commonSituations":"Running 'go mod init' was skipped; the module line was deleted during editing; a go.mod was created by hand without the module directive; a template/generated file omitted it.","solutions":["Run 'go mod edit -module=example.com/yourmod' to add the module declaration.","Or 'go mod init example.com/yourmod' if go.mod itself does not yet exist or should be reinitialized.","Verify the module path follows the rules (domain/path, no spaces)."],"exampleFix":"// before (go.mod)\ngo 1.21\n\nrequire example.com/pkg v1.0.0\n// error: missing module declaration\n\n// after\nmodule example.com/yourmod\n\ngo 1.21\n\nrequire example.com/pkg v1.0.0","handlingStrategy":"validation","validationCode":"// Ensure a module directive exists:\n//   grep -q '^module ' go.mod || echo \"missing module directive\"\n// Or programmatically: f, _ := modfile.Parse(\"go.mod\", data, nil); if f.Module == nil { /* reinit */ }","typeGuard":"func hasModuleDirective(data []byte) bool {\n    f, err := modfile.ParseLax(\"go.mod\", data, nil)\n    return err == nil && f.Module != nil\n}","tryCatchPattern":null,"preventionTips":["Always start a new module with 'go mod init <path>'.","Include a go.mod presence check in project scaffolding templates.","Lint go.mod in CI with 'go mod edit -print >/dev/null'."],"tags":["go-modules","go-mod","configuration","missing-directive"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}