How to Fix Golang 'go.mod File Not Found' Errors
How to fix the Go error 'go.mod file not found in current directory or any parent directory', on a real Go program: why go run main.go works and go run . doesn't, what go.mod is, go mod init and go mod tidy, why the folder you run from matters (Go looks up, never down), go env GOMOD, a go.work workspace for two modules, and the same error inside a Docker build.
One file runs, the folder doesn't
Your Go program stops with go.mod file not found, and you're sure you're in the right folder. Sue runs the departure board at a commuter rail station. It's a small Go program. go run main.go prints the board. In the same folder, go run. says: go.mod file not found. The trains are on time. The program is not. So what is Go looking for? And why can it fail three more ways, even with go.mod right there?

The usual fix: go mod init
go run main.go builds just that one file, and the board prints. go run. asks for the whole folder as a project. The dot means this folder. A project needs a go.mod file, and this folder doesn't have one. The usual fix is one command: go mod init. Typed with nothing after it, it stops: cannot determine module path. Go wants a name for the project, any name you like. Sue uses the folder's: go mod init departures. Go creates go.mod, and now go run. prints the board.
go run main.goRIVERSIDE STATION - DEPARTURES 08:14 Harbor Line platform 2 on time 08:22 Valley Line platform 1 delayed 4 min
go run .go: go.mod file not found in current directory or any parent directory; see 'go help modules'
go mod initgo: cannot determine module path for source directory /home/sue/departures (outside GOPATH, module path must be specified) Example usage: 'go mod init example.com/m' to initialize a v0 or v1 module 'go mod init example.com/m/v2' to initialize a v2 module Run 'go help mod init' for more information.
go mod init departuresgo: creating new go.mod: module departures go: to add module requirements and sums: go mod tidy
go run .RIVERSIDE STATION - DEPARTURES 08:14 Harbor Line platform 2 on time 08:22 Valley Line platform 1 delayed 4 min
What go.mod is for
So what is go.mod? A short text file that describes a module. A module is Go's word for a project: a folder of Go code that's built together, plus a list of the outside code it uses. The first line names it: departures. The last line is the Go version it was made with. And one command shows which go.mod Go is using right now: go env GOMOD. Here, the one in Sue's folder. Keep that command in mind.
cat go.modmodule departures go 1.27.4
go env GOMOD/home/sue/departures/go.mod
Think of a ring binder. Its cover sheet names the project, and every page inside belongs to it. You can read one loose page on its own: that's go run main.go. But go run. means: the whole binder. With no cover sheet, Go doesn't know which project it's holding, so it stops.

go.mod has a second job. Sue wants the words on time to show in green. So at the top of her file, she imports a package called color, which means her code borrows it. A package is ready-made Go code someone else wrote, and this one lives on GitHub. Guess first: go.mod is there now. Does go run. work? No. The error says: no required module provides this package. A different error from the title, but still about go.mod. go.mod lists every outside package the project uses, and color isn't on the list. go mod tidy reads the imports, downloads color and the three packages it needs, and writes them into go.mod.
sed -n '3,7p' main.goimport ( "fmt" "github.com/fatih/color" )
go run .main.go:6:2: no required module provides package github.com/fatih/color; to add it: go get github.com/fatih/color
go mod tidygo: finding module for package github.com/fatih/color go: downloading github.com/fatih/color v1.19.0 go: found github.com/fatih/color in github.com/fatih/color v1.19.0 go: downloading github.com/mattn/go-colorable v0.1.14 go: downloading github.com/mattn/go-isatty v0.0.20 go: downloading golang.org/x/sys v0.42.0
cat go.modmodule departures go 1.27.4 require github.com/fatih/color v1.19.0 require ( github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect golang.org/x/sys v0.42.0 // indirect )
Now it runs, and on time is green. One catch: go run main.go works without go.mod only as long as the file imports nothing from outside. Import a package like color, and it fails too, because only go.mod can say which version to download. So beyond one small file, you need a module.
go run .RIVERSIDE STATION - DEPARTURES 08:14 Harbor Line platform 2 on time 08:22 Valley Line platform 1 delayed 4 min
cd /tmp/loose
go run main.gomain.go:6:2: no required module provides package github.com/fatih/color: go.mod file not found in current directory or any parent directory; see 'go help modules'
Run from the wrong folder
Now the most common way to hit this error with a go.mod that exists: the folder you run from. Sue's project has a tools folder inside it, holding a second small program. Guess first: from inside tools, does go run. find go.mod? Yes. It prints the next train, and go env GOMOD names the go.mod one folder up. Now she goes to her home folder, and runs go run./departures, pointing at the project from outside. Same project: go.mod not found. And go env GOMOD now says /dev/null. That's Go's way of saying: I found no go.mod, so there's no module at all.
go run .next: 08:14 Harbor Line, platform 2
go env GOMOD/home/sue/departures/go.mod
cd ~
go run ./departuresgo: go.mod file not found in current directory or any parent directory; see 'go help modules'
go env GOMOD/dev/null
Here's the rule behind the error. Go starts in the folder you're in, and looks for go.mod there. If it isn't there, Go climbs to the parent folder, then the next, all the way to the top of the disk. It never looks down into the folders below. From inside tools, it finds Sue's go.mod one level up. Pointing at a folder, like./departures, doesn't change where Go starts. From her home folder, it climbs to the top and gives up.

The Go Modules Reference puts it in one line. The main module, the project you are running, is defined by a go.mod file in the current directory, or a parent directory. Current, or parent. Never a folder below.

Two projects in one folder
Now two projects side by side, run from one top folder. Rich runs the station's alert screens. His folder, rail, holds two projects, board and alerts, each with its own go.mod. From the top folder, he runs go run./alerts, and gets go.mod file not found. rail itself has no go.mod, and Go doesn't look down into alerts. He could change folder into alerts, with cd. Or he can stay at the top: go work init lists both folders in a file called go.work. That makes a workspace, which tells Go these two projects belong together. Now go run./alerts finds it, and the alert prints.
ls */alerts/: go.mod main.go board/: go.mod main.go
go run ./alertsgo: go.mod file not found in current directory or any parent directory; see 'go help modules'
go work init ./board ./alerts
cat go.workgo 1.27.4 use ( ./alerts ./board )
go run ./alertsALERT: Valley Line delayed 4 min
Inside a Docker build
The last place this error hides is a Docker build. Sue packages the board as a container image: a sealed copy of the app that runs anywhere. The recipe for it is a file called a Dockerfile. Hers starts from the official Go image, copies main.go in, and builds. It fails with go.mod file not found. Her laptop has go.mod. But a Docker build only sees the files the Dockerfile copies in, and this one copied only main.go.
cat DockerfileFROM golang:1.27 WORKDIR /app COPY main.go . RUN go build -o board . CMD ["./board"]
docker build -t board . 2>&1 | tail -12> [4/4] RUN go build -o board .: 0.121 go: go.mod file not found in current directory or any parent directory; see 'go help modules' ------ Dockerfile:4 -------------------- 2 | WORKDIR /app 3 | COPY main.go . 4 | >>> RUN go build -o board . 5 | CMD ["./board"] 6 | -------------------- ERROR: failed to build: failed to solve: process "/bin/sh -c go build -o board ." did not complete successfully: exit code: 1
The fix: first, copy in go.mod and go.sum. go.sum is a second file that go mod tidy made. It holds a fingerprint of each download. Then download the packages they list. Then copy in the rest of the code. Copying go.mod first lets Docker reuse those downloads next time.

The build finishes, and docker run starts the image once, printing the board from inside the container.
docker build -t board . 2>&1 | tail -2#11 unpacking to docker.io/library/board:latest 0.2s done #11 DONE 0.3s
docker run --rm boardRIVERSIDE STATION - DEPARTURES 08:14 Harbor Line platform 2 on time 08:22 Valley Line platform 1 delayed 4 min
Four checks
Four checks, in order. First, where am I? Run go env GOMOD. If it says /dev/null, Go found no module. Second, no go.mod yet? go mod init with a name, then go mod tidy. Third, wrong folder? cd into the project, or add a go.work at the top. Fourth, in Docker, copy go.mod in before you build.

Recap
So: go.mod file not found means Go looked in the folder you're in and every folder above, and found no cover sheet. Make one, run from inside the project, or copy it into your Docker build.

If this got your Go project running, subscribing brings more fixes, each run for real.
Every command here ran for real, and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for a laptop, whose docker command used the Mac's Docker engine; the rail station is made up. Every command, error and output was real. Watch the full video on YouTube.