Cloud Explainers

How to Fix Golang 'go.mod File Not Found' Errors

Go (Golang)8 min video

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?

Same folder, two answers: comparison table of go run main.go vs go run., The error, The folder, The project, The answer, The trains, What's missing?

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.

Sue's laptop: ~/departures
go run main.goRIVERSIDE STATION - DEPARTURES
08:14  Harbor Line   platform 2   on time
08:22  Valley Line   platform 1   delayed 4 min
Sue's laptop: ~/departures
go run .go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Sue's laptop: ~/departures
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.
Sue's laptop: ~/departures
go mod init departuresgo: creating new go.mod: module departures
go: to add module requirements and sums:
    go mod tidy
Sue's laptop: ~/departures
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.

Sue's laptop: ~/departures
cat go.modmodule departures

go 1.27.4
Sue's laptop: ~/departures
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.

A Go module is a binder with a cover sheet: comparison table of A ring binder vs A Go module, Names the project, Belongs to it, Read one page, The whole thing

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.

Sue's laptop: ~/departures
sed -n '3,7p' main.goimport (
    "fmt"

    "github.com/fatih/color"
)
Sue's laptop: ~/departures
go run .main.go:6:2: no required module provides package github.com/fatih/color; to add it:
    go get github.com/fatih/color
Sue's laptop: ~/departures
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
Sue's laptop: ~/departures
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.

Sue's laptop: ~/departures
go run .RIVERSIDE STATION - DEPARTURES
08:14  Harbor Line   platform 2   on time
08:22  Valley Line   platform 1   delayed 4 min
Sue's laptop: ~/departures
cd /tmp/loose
Sue's laptop: ~/departures
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.

Sue's laptop: ~/departures
go run .next: 08:14 Harbor Line, platform 2
Sue's laptop: ~/departures
go env GOMOD/home/sue/departures/go.mod
Sue's laptop: ~/departures
cd ~
Sue's laptop: ~/departures
go run ./departuresgo: go.mod file not found in current directory or any parent directory; see 'go help modules'
Sue's laptop: ~/departures
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.

go run. looks for go.mod here, then in every folder above: diagram of /, /home/sue, departures, tools, go run. in tools, go run./departures

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.

Run from the wrong folder: browser screenshot from the video

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.

Rich's login: ~/rail
ls */alerts/:
go.mod  main.go

board/:
go.mod  main.go
Rich's login: ~/rail
go run ./alertsgo: go.mod file not found in current directory or any parent directory; see 'go help modules'
Rich's login: ~/rail
go work init ./board ./alerts
Rich's login: ~/rail
cat go.workgo 1.27.4

use (
    ./alerts
    ./board
)
Rich's login: ~/rail
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.

Sue's laptop: ~/departures
cat DockerfileFROM golang:1.27
WORKDIR /app
COPY main.go .
RUN go build -o board .
CMD ["./board"]
Sue's laptop: ~/departures
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.

Copy go.mod into the build: code change of Dockerfile, COPY go.mod go.sum./ (go.mod goes in), RUN go mod download, COPY.. (then the code)

The build finishes, and docker run starts the image once, printing the board from inside the container.

Sue's laptop: ~/departures
docker build -t board . 2>&1 | tail -2#11 unpacking to docker.io/library/board:latest 0.2s done
#11 DONE 0.3s
Sue's laptop: ~/departures
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.

Four checks for go.mod file not found: cards of Where am I?, No go.mod yet, Wrong folder, Docker 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.

go.mod file not found: nothing here or above: diagram of /, /home/sue, departures, tools, go run. in tools, go run./departures

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.

More videos

PostgreSQL9 min

Every PostgreSQL Setting to Change After You Install It

Docker9 min

What Happens When You Install Docker on Ubuntu

Python8 min

How to Fix Python 'Address Already in Use' Errors

Go (Golang)5 min

Golang Interfaces and Mocks in 5 Minutes

Git7 min

How to Fix Git 'Detached HEAD' State

Linux21 min

Linux Firewall with ufw: Full Tutorial for Beginners

Go (Golang)5 min

Golang defer Statements in 5 Minutes

Auth (OAuth, JWT)9 min

How to Fix OAuth 'invalid_grant' Errors

Databases & SQL11 min

The SQL Roadmap for Beginners

Go (Golang)5 min

Golang make() for Slices and Maps in 5 Minutes

Networking7 min

How to Fix Network 'No Route to Host' Errors on Linux

Linux6 min

How to Fix Linux 'Too Many Open Files' Errors

Django (Python web framework)11 min

Every Django Concept a Beginner Needs to Know

Linux24 min

SSH on Linux: Full Tutorial for Beginners

Go (Golang)5 min

Golang for range Loops in 5 Minutes

Databases & SQL10 min

SQL Databases: SQLite vs PostgreSQL

Django (Python web framework)10 min

The Django Roadmap for Beginners

Go (Golang)5 min

Golang Structs in 5 Minutes

Caching & Redis22 min

Redis: Full Tutorial for Beginners

Go (Golang)5 min

Golang context.Context in 5 Minutes

Databases & SQL7 min

How to Fix MySQL Database Error 1045: Access Denied for User

Go (Golang)5 min

Golang Method Receivers in 5 Minutes

Python8 min

How to Fix Python SSL: CERTIFICATE_VERIFY_FAILED Errors

Django (Python web framework)9 min

How to Fix Django Static Files Not Loading in Production

OpenRouter (one API for many AI models)8 min

OpenRouter vs Direct API Keys: Which One Should You Use?

Go (Golang)5 min

Golang if err != nil Checks in 5 Minutes

AI agents & MCP22 min

MCP Servers: Full Tutorial for Beginners

Go (Golang)5 min

Golang Error Return Values in 5 Minutes

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python10 min

Python uv vs pip: Which One Should You Use?

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index