Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Go: go.mod file not found in current directory

Go 1.16 and later build in module mode. Run commands inside the module root, create a go.mod with go mod init, or set GO111MODULE and GOWORK deliberately.

~4 min readGo
#go#modules#go-mod#toolchain
Go module crate missing its go.mod slab with a module root platform and init tool beside it

Introduction & Quick Summary

A first go build in a fresh directory stops before compiling anything:

code
$ go build go: go.mod file not found in current directory or any parent directory; see 'go help modules'

Quick answer: you are outside a Go module. Since Go 1.16 the toolchain defaults to module mode and refuses to build a package that no go.mod describes.

bash
cd ~/src/myapp # the directory that should own the module go mod init example.com/myapp go build ./...

The module path in go mod init is not a URL that must resolve; it is the import prefix for your packages, so example.com/myapp is fine for a local project and github.com/you/myapp is the convention for something you will publish.

The same message appears for four different reasons, and they need different fixes: no module at all, a module one directory up that you are outside of, a nested module that hides its parent's packages, and a Docker or CI working directory that is not the module root. This guide covers go env GOMOD, the exact commands that need a module and those that do not, workspaces, and why GO111MODULE=off is not the fix.

Core Mechanics, Signatures & Reproducible Setup

A module is a directory tree with a go.mod at its root. The toolchain walks upward from the working directory to find the nearest go.mod; that file defines the module path, the Go version directive, and the dependency requirements.

bash
go env GOMOD # path to the go.mod in use, or /dev/null when there is none go env GO111MODULE # "on" by default since Go 1.16 go env GOWORK # path to a go.work file when workspaces are active go env GOPATH

A reproducible project from nothing:

bash
mkdir -p ~/src/hello && cd ~/src/hello go mod init example.com/hello cat > main.go <<'GO' package main import "fmt" func main() { fmt.Println("hello") } GO go run . # works go build . # works, produces ./hello

Delete go.mod and the second command fails with the message above while go run main.go may still work — the file-argument form compiles a synthetic command-line-arguments package and does not consult a module.

What goes in go.mod:

code
module example.com/hello go 1.24 require ( github.com/google/uuid v1.6.0 ) require github.com/stretchr/testify v1.10.0 // indirect

The go directive sets the language version and the minimum toolchain behaviour; require lists direct dependencies, and // indirect marks the ones pulled in by another module.

Key Comparison Table

CommandNeeds a moduleBehaviour outside one
go build .yesthis error
go run .yesthis error
go run main.gonocompiles the listed files
go test ./...yesthis error
go get module@versionyesthis error (Go 1.22+)
go install module@versionnodownloads and builds to GOBIN
go env, go versionnoalways work
Situationgo env GOMODFix
fresh directory/dev/nullgo mod init <path>
subdirectory of a moduleparent go.modrun the command there
nested modulethe nested go.modremove it or use a workspace
Docker build/dev/nullWORKDIR + copy go.mod first
workspace/dev/null, GOWORK setgo work use ./pkg
ModeSet byConsequence
module modedefaultmodules required, go.sum verified
workspace modego.workseveral modules built together
GOPATH modeGO111MODULE=offlegacy imports, no go.mod needed

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. go run main.go works but go build . fails

The file form is a special case: Go compiles exactly the files you list, without module context. Any import that needs resolving through a module fails instead:

bash
go run main.go # ok for a single file with stdlib imports go run main.go helper.go # several files, still no module go build . # needs go.mod

If a scratch file imports a third-party package, it needs a module even for go run.

2. Nested modules hide their parent

Running go mod init inside a subdirectory creates a second module. The parent module then cannot see those packages, and the child cannot import the parent's without a replace:

bash
# repo/ # go.mod module example.com/repo # internal/util/ importable from repo # tools/go.mod module example.com/tools <- separate module

Confirm which module a package belongs to before adding imports:

bash
cd tools && go env GOMOD # .../repo/tools/go.mod cd .. && go list ./... # tools/ is not listed

Delete the nested go.mod if it was accidental, or keep it deliberately and add a go.work at the repository root.

3. Workspaces for multi-module repositories

go.work builds several modules as one unit without publishing them:

bash
go work init ./service ./shared go work use ./tools go work sync

Inside the workspace, a package in service can import shared at its declared module path even before it is tagged. GOWORK=off disables the file for one command, which is useful when a CI job must build a module exactly as a consumer sees it.

4. Docker builds

A common image build fails at go mod download because the source was copied before the module file, or because WORKDIR points at /app while go.mod sits in /app/cmd:

dockerfile
FROM golang:1.24-alpine AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app

The copy order is not cosmetic: it keeps the dependency layer cached and makes the failure message point at the right file when go.mod is missing from the build context (for example, excluded by .dockerignore).

5. Installing tools

go install with a version suffix works outside any module and is the supported way to get CLI tools:

bash
go install golang.org/x/tools/cmd/stringer@latest go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.27.0

go install ./cmd/foo has no version suffix and therefore needs a module. Put tool dependencies in a separate tools.go file guarded by a build tag when the version must be pinned by the module graph:

go
//go:build tools package tools import _ "golang.org/x/tools/cmd/stringer"

6. GOPATH mode and legacy projects

GO111MODULE=off restores GOPATH mode, where packages are found under $GOPATH/src and no go.mod is consulted:

bash
GO111MODULE=off go build # legacy behaviour

This is a diagnosis tool, not a fix. A project that only builds this way has imports that assume $GOPATH/src/github.com/you/project, and moving it forward means adding a go.mod and rewriting the import prefix once.

The regular expression tester is handy for turning go list ./... output into a list of import paths, and the text diff tool shows what a go mod tidy run actually changed in go.mod and go.sum.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Running go build in $HOME or /. The toolchain searches upward and finds nothing; cd into the project instead of initialising a module in a broad directory.
  • go mod init with a path you do not own. A published module must use a path that resolves to the repository; a local one can be anything, but changing it later means rewriting imports.
  • Adding go.mod inside cmd/. Every subdirectory becomes its own module; keep one module per repository unless there is a reason.
  • GO111MODULE=off to silence the error. It hides the problem and breaks dependency versioning.
  • Editing go.sum by hand. go mod tidy or go mod download regenerates it; manual edits produce checksum mismatches later.
  • Go 1.24 and toolchain directives. A go line newer than the installed toolchain triggers a download through GOTOOLCHAIN; in offline CI, pin the line to the toolchain you ship.
  • vendor/ without go.mod. A vendored tree is only consulted in module mode; the directory alone does not make a module.

Official documentation: Go modules reference and Tutorial: create a module.

Official references

primary documentation

The Go documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Go guides

More in Go

All Go guides

The guides filed next to this one in Go, in library order.