Skip to content
no required module provides package ...

no required module provides package ...

DodaTech 3 min read

The “no required module provides package” error occurs when Go cannot find a module that contains the package you are trying to import. This typically means your project has no go.mod file or the module path is misconfigured.

What It Means

Go modules are the dependency management system introduced in Go 1.16. When you import a package, Go looks for a go.mod file that declares the module path and its dependencies. If Go cannot find a matching module, it reports that no required module provides the requested package.

Why It Happens

  • Your project does not have a go.mod file. The go mod init command was never run.
  • The import path in your source code does not match the module path declared in go.mod.
  • The imported package is an external dependency that has not been added to go.mod.
  • The package is in a subdirectory but the module path in go.mod is incorrect.
  • You are trying to import a local package without using a proper module structure.

How to Fix It

1. Initialize a Go module

Navigate to your project root and run:

cd /path/to/your/project
go mod init example.com/your-project

This creates a go.mod file that declares your module path. Choose a unique module path that reflects your repository location.

2. Add missing dependencies

Run go mod tidy to scan your source code and add any missing dependencies:

go mod tidy

This command downloads missing modules and removes unused ones.

3. Verify the module path matches your imports

Check that the module path in go.mod matches the import paths used in your code:

# View your go.mod file
cat go.mod
module example.com/your-project
go 1.22

If your source code imports example.com/your-project/utils, ensure the module path is exactly example.com/your-project so that the subdirectory utils resolves to example.com/your-project/utils.

4. Check for module name mismatches

If you renamed your module or cloned a project, update the module path:

# Edit go.mod to match the new path
module github.com/your-user/your-project

Then update all import statements in your code to use the new module path.

5. Add external dependencies explicitly

To add an external package to your project:

go get github.com/gorilla/mux
go mod tidy

This downloads the package and records it in go.mod and go.sum.

FAQ

What is the difference between go mod init and go mod tidy?
go mod init creates a new go.mod file. go mod tidy ensures the go.mod file matches your source code by adding missing dependencies and removing unused ones. Run go mod tidy whenever you import a new package.
Can I have multiple modules in one repository?
Yes. Each module needs its own go.mod file in its own directory. Use replace directives in go.mod to link local modules, or publish them separately.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro