Fix: Go cannot find package / no required module provides package
Quick Answer
How to fix 'cannot find package' and 'no required module provides package' errors in Go by syncing dependencies, fixing import paths, configuring private repos, and resolving workspace issues.
The Error
You run go build, go run, or go test and hit one of these messages:
Module dependency missing:
main.go:4:2: no required module provides package github.com/redis/go-redis/v9; to add it:
go get github.com/redis/go-redis/v9Package not found anywhere:
cannot find package "github.com/sirupsen/logrus" in any of:
/usr/local/go/src/github.com/sirupsen/logrus (from $GOROOT)
/home/user/go/src/github.com/sirupsen/logrus (from $GOPATH)Module resolution failure:
go: cannot find module providing package github.com/jackc/pgx/v5Missing main module:
go: cannot find main module, but found .git/config in /home/user/project
to create a module there, run:
go mod initThese errors all point to the same root problem: Go cannot locate a package your code imports. The fix depends on what’s actually wrong — a missing dependency, a bad import path, a module configuration issue, or an environment problem.
Why This Happens
Go’s module system (the default since Go 1.16) resolves packages through a chain of lookups. When any link in that chain breaks, you get a “cannot find package” error. Here is what Go checks, in order:
- The standard library. Go looks for the package in
$GOROOT/src/. If you importfmtornet/http, it comes from here. - The
go.modfile. Go reads your module’sgo.modto find which version of a dependency to use. If the package isn’t listed (directly or transitively), resolution fails. - The module cache. Downloaded modules live in
$GOPATH/pkg/mod/. If the module isn’t cached, Go fetches it from the module proxy. - The module proxy. By default, Go hits
proxy.golang.orgto download modules. If the proxy can’t find the module (or can’t access it), the fetch fails. - Direct source. If the proxy falls through to
direct, Go tries togit clone(orhg clone, etc.) the repository directly.
The error fires when every step in this chain fails for your package. Common causes include:
- No
go.modfile. The project hasn’t been initialized as a Go module. - Dependency not in
go.mod. You added an import statement but never rango getorgo mod tidy. - Wrong import path. A typo, wrong casing, or missing major version suffix (
/v2) in the import. - Module path mismatch in
go.mod. Yourgo.moddeclares a different module path than what your imports reference. - GOPATH vs modules confusion.
GO111MODULEis set tooff, forcing legacy GOPATH mode. - Private repository. The module proxy and checksum database cannot reach private repos.
- Stale vendor directory. The
vendor/folder doesn’t include the new dependency. - Workspace mode interference. A
go.workfile is changing module resolution.
Fix 1: Run go mod tidy to Sync Dependencies
This is the fix that works 80% of the time. Run it first:
go mod tidygo mod tidy scans every .go file in your module, finds all import statements, adds missing dependencies to go.mod, removes unused ones, and updates go.sum. It’s the single command that keeps your dependency graph consistent.
If you don’t have a go.mod file yet, create one first:
go mod init github.com/yourname/yourproject
go mod tidyFor projects that won’t be imported by others, any module path works:
go mod init myapp
go mod tidyAfter running go mod tidy, try building again. If the error is gone, you’re done. If not, the cause is deeper — keep reading.
Note: go mod tidy requires network access to resolve new dependencies. If you’re behind a firewall or offline, see Fix 6 (private repos) or the proxy section under “Still Not Working?”
Fix 2: Run go mod download to Fetch Modules
Sometimes your go.mod and go.sum are correct, but the modules aren’t in your local cache. This happens after cloning a repo, switching machines, or clearing the module cache:
go mod downloadThis downloads every module listed in go.mod without modifying any files. It’s a pure fetch operation.
To download a specific module:
go mod download github.com/redis/go-redis/v9To verify downloads match their expected checksums:
go mod download -xThe -x flag prints the commands Go executes, which is useful for debugging network or authentication issues.
If go mod download fails with a 410 or 404 error, the module might not exist on the proxy. Check the import path is correct and try fetching directly:
GOPROXY=direct go mod downloadPro Tip: Run
go mod downloadin your CI/CD pipeline as a separate caching step beforego build. This lets you cache the module download independently from the build, speeding up subsequent pipeline runs significantly. Most CI systems (GitHub Actions, GitLab CI) can cache$GOPATH/pkg/modbetween jobs.
Fix 3: Fix Your go.mod Module Path
The module path in go.mod must match what your import statements use. If they diverge, Go can’t resolve internal packages.
Check your go.mod:
module github.com/yourname/myapp
go 1.22Every import of an internal package must use that exact module path as its prefix:
// go.mod declares: module github.com/yourname/myapp
// Correct
import "github.com/yourname/myapp/internal/database"
import "github.com/yourname/myapp/pkg/utils"
// Wrong -- using a short name instead of the module path
import "myapp/internal/database"
// Wrong -- different casing
import "github.com/YourName/MyApp/internal/database"Common mistakes with go.mod module paths:
Trailing slash:
// Wrong
module github.com/yourname/myapp/
// Correct
module github.com/yourname/myappMismatch after renaming a repo:
If you renamed your GitHub repository from old-name to new-name, update go.mod:
// Old
module github.com/yourname/old-name
// New
module github.com/yourname/new-nameThen update every import statement across your codebase. Use your editor’s find-and-replace or a tool like gofmt with sed:
find . -name '*.go' -exec sed -i 's|github.com/yourname/old-name|github.com/yourname/new-name|g' {} +After changing the module path, run go mod tidy to clean up.
Fix 4: Fix Import Path Typos and Case Sensitivity
Go import paths are case-sensitive and must exactly match the module’s declared path. Even one wrong character causes a “cannot find package” error.
Wrong casing:
// Wrong
import "github.com/Sirupsen/logrus"
// Correct (the author changed the GitHub username to lowercase)
import "github.com/sirupsen/logrus"Missing sub-package path:
// Wrong -- this is the module root, not an importable package
import "github.com/aws/aws-sdk-go-v2"
// Correct -- import the actual package you need
import "github.com/aws/aws-sdk-go-v2/config"
import "github.com/aws/aws-sdk-go-v2/service/dynamodb"Missing major version suffix:
Go requires modules at v2 or higher to include the major version in the import path. This is a frequent source of confusion and is related to how Go modules resolve versions.
// Wrong -- this gets v0 or v1
import "github.com/jackc/pgx"
// Correct -- v5 requires the /v5 suffix
import "github.com/jackc/pgx/v5"The require line in go.mod must also use the suffix:
require github.com/jackc/pgx/v5 v5.5.0Vanity import paths:
Some projects use custom import paths that differ from their repository URL. For example, go.uber.org/zap is hosted at github.com/uber-go/zap, but you must import it using the vanity URL:
// Correct
import "go.uber.org/zap"
// Wrong -- this is the GitHub repo URL, not the import path
import "github.com/uber-go/zap"Check the project’s documentation or go.mod file for the correct import path.
Common Mistake: Copying import paths from old blog posts or Stack Overflow answers. Module paths change over time — authors rename repos, migrate to vanity URLs, or release major versions. Always check the module’s current documentation or
pkg.go.devpage for the correct import path.
Fix 5: Fix GOPATH vs Go Modules Confusion
If your error mentions $GOPATH instead of module-related messages, you might be stuck in legacy GOPATH mode. Check your GO111MODULE setting:
go env GO111MODULEon(default since Go 1.16): Go modules are always active. This is correct.off: Modules are disabled. Go looks for packages in$GOPATH/src/using the old vendoring model.auto: Go uses modules only if ago.modfile exists in the current or parent directory.
If it’s off, fix it:
go env -w GO111MODULE=onOr set it for the current shell session:
export GO111MODULE=onOn Windows (PowerShell):
$env:GO111MODULE = "on"This issue typically appears when working with old CI configurations, legacy Dockerfiles, or tutorials written before Go 1.16. If you find GO111MODULE=off in a Dockerfile or CI script, remove it. Modern Go (1.21+) defaults to on and rarely needs this variable set at all.
If you’re working on a genuinely legacy project that hasn’t been migrated to modules, initialize it:
cd /path/to/project
go mod init github.com/yourname/project
go mod tidyThis creates go.mod and go.sum, converting the project from GOPATH mode to modules. Review the generated go.mod to ensure dependencies look correct, similar to how you’d verify unused variables after a refactor.
Fix 6: Fix Private Repository Access
When you import a package from a private GitHub, GitLab, or Bitbucket repository, the default module proxy (proxy.golang.org) can’t access it. You’ll see:
go: module github.com/yourcompany/internal-lib: reading https://proxy.golang.org/...: 410 GoneOr a checksum mismatch:
verifying github.com/yourcompany/[email protected]: checksum mismatchSet GOPRIVATE
Tell Go to bypass the proxy and checksum database for your private modules:
go env -w GOPRIVATE=github.com/yourcompany/*Multiple patterns are comma-separated:
go env -w GOPRIVATE=github.com/yourcompany/*,gitlab.internal.com/*GOPRIVATE is shorthand that sets both GONOPROXY (skip the proxy) and GONOSUMDB (skip the checksum database) for matching modules.
Configure Git authentication
Go uses git to fetch modules directly when the proxy is bypassed. Your Git client needs credentials for private repos.
HTTPS with a personal access token (GitHub):
git config --global url."https://[email protected]/".insteadOf "https://github.com/"SSH instead of HTTPS:
git config --global url."[email protected]:".insteadOf "https://github.com/"Make sure your SSH key is added to your GitHub account. If you’re troubleshooting SSH authentication, see Fix: Git Permission Denied (publickey).
GONOSUMCHECK for checksum bypass
If you need to skip checksum verification for specific modules without skipping the proxy:
go env -w GONOSUMDB=github.com/yourcompany/*Warning: Only skip checksum verification for modules you trust. The checksum database protects against supply-chain attacks and tampered dependencies.
CI/CD considerations
In CI pipelines, use environment variables instead of go env -w:
# GitHub Actions example
env:
GOPRIVATE: github.com/yourcompany/*
GONOSUMDB: github.com/yourcompany/*Set up authentication using a machine user token or deploy key, not personal credentials.
Fix 7: Fix Vendor Directory Issues
If your project uses go mod vendor for vendored dependencies, the vendor/ directory must stay in sync with go.mod. When they diverge, you get:
cannot find package "github.com/example/lib" in any of:
/home/user/myapp/vendor/github.com/example/libRegenerate the vendor directory
go mod vendorThis deletes and recreates vendor/ based on the current go.mod. Then build with vendoring enabled:
go build -mod=vendor ./...Force vendor mode globally
If your project always uses vendoring:
go env -w GOFLAGS=-mod=vendorNow every go build, go test, and go run command uses the vendor directory automatically.
When vendor mode kicks in
Go auto-detects the vendor/ directory only in the main module and only if go.mod declares Go 1.14 or later. If your go.mod has an older version, Go ignores vendor/:
// Update this if it's old
go 1.22Common vendor pitfalls
- Adding a dependency but forgetting
go mod vendor. You rungo get github.com/new/lib, but the vendor directory still lacks the new package. - Committing partial vendor updates. If
go mod vendorwas interrupted or you only committed some files, the vendor directory is inconsistent. Regenerate it fully and commit everything. - Mixing vendor and non-vendor builds. If some team members use
-mod=vendorand others don’t, builds can diverge. Agree on one approach and enforce it in CI.
Fix 8: Fix Go Workspace Mode (go.work)
Go 1.18 introduced workspaces for developing multiple modules simultaneously. A go.work file in your directory tree changes how Go resolves modules, which can cause unexpected “cannot find package” errors.
Detect workspace mode
go env GOWORKIf this prints a file path, workspace mode is active. Go resolves modules through go.work instead of individual go.mod files.
The problem
You have a go.work file that lists some modules but not all. If your module isn’t included in go.work, Go can’t find its packages:
// go.work
go 1.22
use (
./service-a
./service-b
)
// ./service-c is missing -- its packages won't resolveAdd the missing module
go work use ./service-cOr edit go.work directly:
go 1.22
use (
./service-a
./service-b
./service-c
)Disable workspace mode
If you don’t need workspaces and go.work is causing problems, disable it for a single command:
GOWORK=off go build ./...Or remove the workspace files entirely:
rm go.work go.work.sumWhen to use workspaces
Workspaces are useful when you’re developing a library and a service that depends on it simultaneously. Instead of using replace directives in go.mod (which you’d need to remove before committing), go.work handles the local override cleanly.
A typical go.work setup looks like this:
go 1.22
use (
./my-service
./my-library
)Now my-service automatically uses the local version of my-library without any replace directives. This is especially relevant when working with module resolution issues across multiple packages.
Note: Don’t commit go.work to your repository unless your team has agreed on a shared workspace layout. Add it to .gitignore for local development.
Still Not Working?
If none of the fixes above solved the error, try these less common solutions.
Clear the module cache
A corrupted module cache can cause bizarre resolution failures:
go clean -modcache
go mod tidyThis deletes everything in $GOPATH/pkg/mod/ and re-downloads all dependencies. It’s a clean slate.
Fix proxy issues (GOPROXY)
Check your proxy configuration:
go env GOPROXYThe default is https://proxy.golang.org,direct. If it’s set to something else (or off), reset it:
go env -w GOPROXY=https://proxy.golang.org,directIf the default proxy is blocked (corporate firewall, regional restrictions), try a mirror:
# Common in China
go env -w GOPROXY=https://goproxy.cn,directOr bypass the proxy entirely:
go env -w GOPROXY=directFix checksum database errors
If you see:
verifying github.com/example/[email protected]: checksum mismatchDelete go.sum and regenerate it:
rm go.sum
go mod tidyIf the error persists, the module’s contents may have genuinely changed (a re-tagged release, for instance). Use GONOSUMDB for that specific module:
go env -w GONOSUMDB=github.com/example/libUse replace directives for local development
If you’re developing a dependency locally and want to test changes without publishing, add a replace directive to go.mod:
replace github.com/yourcompany/shared-lib => ../shared-libThe local path must contain its own go.mod. After testing, remove the replace directive before committing. For a cleaner approach across multiple modules, consider using go.work (Fix 8) instead.
For forks, point to the fork repository:
replace github.com/original/lib => github.com/yourfork/lib v1.2.1-fixedCheck your Go version
Some modules require a minimum Go version. Verify yours:
go versionIf a module’s go.mod declares go 1.22 and you’re running Go 1.20, the build will fail. Either update Go or pin an older version of the dependency. This is a common issue when working on older codebases, similar to encountering undefined variable errors after a language version upgrade.
Verify the module exists on pkg.go.dev
Search for the package at pkg.go.dev. If it doesn’t appear, the module path might be wrong, the repository might be private, or the module might have been deleted. Check the repository directly and verify the go.mod file at the root declares the module path you expect.
IDE showing errors but build succeeds
If go build works from the terminal but your IDE shows “cannot find package” errors, restart the language server:
- VS Code: Open Command Palette, run “Go: Restart Language Server”
- GoLand: File > Invalidate Caches > Invalidate and Restart
If the language server is outdated, update it:
go install golang.org/x/tools/gopls@latestThis is unrelated to the actual build system — it’s a tooling sync issue. Your code compiles fine; the IDE just needs to catch up. A similar category of tooling mismatch occurs when type errors appear only in the editor but not during compilation.
Related: For the closely related “no required module provides package” variant with more edge cases, see Fix: no required module provides package. For module-level resolution failures, see Fix: Go Module Not Found.
Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.
Was this article helpful?
Related Articles
Fix: Go fatal error: all goroutines are asleep - deadlock!
How to fix Go fatal error all goroutines are asleep deadlock caused by unbuffered channels, missing goroutines, WaitGroup misuse, and channel direction errors.
Fix: Go panic: runtime error: index out of range
How to fix Go panic runtime error index out of range caused by empty slices, off-by-one errors, nil slices, concurrent access, and missing bounds checks.
Fix: Go cannot use X (type Y) as type Z in argument
How to fix Go 'cannot use as type' error caused by type mismatches, interface satisfaction, pointer vs value receivers, type conversions, and generic constraints.
Fix: Go undefined: variable (or function)
How to fix Go undefined error caused by undeclared variables, wrong package scope, unexported names, missing imports, build tags, and file organization issues.