go-kratos/kratos Β· error

🚫 go.mod don't exists in %s

Error message

🚫 go.mod don't exists in %s

What it means

Kratos CLI error raised in no-module mode: when adding a subproject (kratos new <name> with nomod), the CLI walks up from the working directory to find a project root containing go.mod. If none exists, the new package cannot be given a module-relative import path, so creation aborts with this error.

Source

Thrown at cmd/kratos/internal/project/project.go:90

	done := make(chan error, 1)
	var repoURL string
	if repo != "" {
		repoURL = repo
	} else {
		repoURL, err = selectRepo()
		if err != nil {
			fmt.Fprintf(os.Stderr, "\033[31mERROR: failed to select repo(%s)\033[m\n", err.Error())
			return
		}
	}
	go func() {
		if !nomod {
			done <- p.New(ctx, workingDir, repoURL, branch)
			return
		}
		projectRoot := getgomodProjectRoot(workingDir)
		if gomodIsNotExistIn(projectRoot) {
			done <- fmt.Errorf("🚫 go.mod don't exists in %s", projectRoot)
			return
		}

		packagePath, e := filepath.Rel(projectRoot, filepath.Join(workingDir, projectName))
		if e != nil {
			done <- fmt.Errorf("🚫 failed to get relative path: %v", e)
			return
		}
		packagePath = strings.ReplaceAll(packagePath, "\\", "/")

		mod, e := base.ModulePath(filepath.Join(projectRoot, "go.mod"))
		if e != nil {
			done <- fmt.Errorf("🚫 failed to parse `go.mod`: %v", e)
			return
		}
		// Get the relative path for adding a project based on Go modules
		p.Path = filepath.Join(strings.TrimPrefix(workingDir, projectRoot+"/"), p.Name)
		done <- p.Add(ctx, workingDir, repoURL, branch, mod, packagePath)

View on GitHub (pinned to 668db92c2c)

Solutions

  1. cd into the intended parent project (the one with go.mod) and rerun the command
  2. If no module exists yet: run go mod init <module-path> in the intended parent, then rerun with nomod
  3. Or drop the nomod mode so the CLI scaffolds a standalone project with its own go.mod

Example fix

# before
$ cd ~ && kratos new users --nomod
# go.mod don't exists in /home/me

# after
$ mkdir shop && cd shop && go mod init github.com/me/shop
$ kratos new users --nomod
Defensive patterns

Strategy: validation

Validate before calling

// before shelling out to kratos new in no-module mode
if _, err := os.Stat("go.mod"); err != nil {
    log.Fatal("no go.mod here: run `go mod init <path>` or drop the nomod mode")
}

Prevention

When it happens

Trigger: Running kratos new <name> in no-module mode from a directory where no ancestor directory contains go.mod β€” getgomodProjectRoot(workingDir) returns a root for which gomodIsNotExistIn is true.

Common situations: Running the CLI from $HOME or /tmp; forgetting go mod init in a fresh repo; scaffolding a sub-service before the parent module exists; wrong terminal cwd.

Related errors


AI-assisted analysis of go-kratos/kratos@668db92c2c (2026-08-16). Data as JSON: /api/errors/4cbb08ba53fc043a. Report an issue: GitHub.