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.

Introduction & Quick Summary
A first go build in a fresh directory stops before compiling anything:
$ 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.
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.
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 GOPATHA reproducible project from nothing:
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 ./helloDelete 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:
module example.com/hello
go 1.24
require (
github.com/google/uuid v1.6.0
)
require github.com/stretchr/testify v1.10.0 // indirectThe 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
| Command | Needs a module | Behaviour outside one |
|---|---|---|
go build . | yes | this error |
go run . | yes | this error |
go run main.go | no | compiles the listed files |
go test ./... | yes | this error |
go get module@version | yes | this error (Go 1.22+) |
go install module@version | no | downloads and builds to GOBIN |
go env, go version | no | always work |
| Situation | go env GOMOD | Fix |
|---|---|---|
| fresh directory | /dev/null | go mod init <path> |
| subdirectory of a module | parent go.mod | run the command there |
| nested module | the nested go.mod | remove it or use a workspace |
| Docker build | /dev/null | WORKDIR + copy go.mod first |
| workspace | /dev/null, GOWORK set | go work use ./pkg |
| Mode | Set by | Consequence |
|---|---|---|
| module mode | default | modules required, go.sum verified |
| workspace mode | go.work | several modules built together |
| GOPATH mode | GO111MODULE=off | legacy 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:
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.modIf 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:
# repo/
# go.mod module example.com/repo
# internal/util/ importable from repo
# tools/go.mod module example.com/tools <- separate moduleConfirm which module a package belongs to before adding imports:
cd tools && go env GOMOD # .../repo/tools/go.mod
cd .. && go list ./... # tools/ is not listedDelete 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:
go work init ./service ./shared
go work use ./tools
go work syncInside 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:
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/appThe 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:
go install golang.org/x/tools/cmd/stringer@latest
go install github.com/sqlc-dev/sqlc/cmd/sqlc@v1.27.0go 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: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:
GO111MODULE=off go build # legacy behaviourThis 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 buildin$HOMEor/. The toolchain searches upward and finds nothing;cdinto the project instead of initialising a module in a broad directory. go mod initwith 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.modinsidecmd/. Every subdirectory becomes its own module; keep one module per repository unless there is a reason. GO111MODULE=offto silence the error. It hides the problem and breaks dependency versioning.- Editing
go.sumby hand.go mod tidyorgo mod downloadregenerates it; manual edits produce checksum mismatches later. - Go 1.24 and toolchain directives. A
goline newer than the installed toolchain triggers a download throughGOTOOLCHAIN; in offline CI, pin the line to the toolchain you ship. vendor/withoutgo.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 documentationThe Go documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Go guides
All Go guidesGo: x509 certificate signed by unknown authority
Go cannot build a chain to a trusted root: no CA bundle in the image, a private CA, or a missing intermediate. Install roots or set RootCAs.
Go: fatal error: concurrent map writes in goroutines
The runtime catches two writes to one map and stops the process. Guard access with a mutex, use sync.Map for caches, or give one owner.
Go: import cycle not allowed
Go forbids cycles in the package import graph. Break them by extracting shared types, declaring interfaces where consumed, or inverting init registration.
Go: http: superfluous response.WriteHeader call
net/http logs this when a handler writes the status twice. It almost always marks a later status write that was silently ignored, or no return.
More in Go
All Go guidesThe guides filed next to this one in Go, in library order.