The go: go.mod file not found in current directory or any parent directory error occurs when you are trying to “run a Go command that expects the current directory (or a parent directory) to be inside a Go module, but no go.mod file has been found.”
To fix the error in Golang, you can create a go.mod file using the go mod init <module_name> command or use this command: go env -w GO111MODULE=off.
Error and solution diagram
Let’s initialize a go module using the below command in your project’s root directory:
go mod init <module_name>
Replace <module_name> with a name for your module. Typically, the module name is the repository URL where your code will be hosted. For example:
go mod init github.com/yourusername/yourproject
This command will create a go.mod file in the current directory with the provided module name. The go.mod file manages your project’s dependencies and other module-related settings.
After initializing your project as a Go module, you can use Go commands like go build, go test, and go run without encountering the go.mod file not found error.
Alternate solutions
Check Your Current Directory
Ensure you are in the correct directory of your Go project. Sometimes, the error can result from simply being in the wrong directory.
Recursive Search
If you believe that there’s a go.mod file somewhere in a parent directory, navigate to the root of your file system and search for it:
find / -name go.mod
If you find it, navigate to that directory and try rerunning your Go command.
Not Using Go Modules
Suppose you’re working with an older project that doesn’t use modules. In that case, you might consider either initializing a new module (as described above) or ensuring you have the correct GOPATH and working outside the module system. However, as Go continues to evolve, adopting the module system is recommended.
Go Version
Ensure that you’re using Go 1.11 or newer. Go modules were introduced in Go 1.11, so if you’re using an older version, you won’t have support for modules.
Commands Outside a Module
Some Go commands can run outside a module, while others cannot. For instance, go build can compile a single-file Go program without a go.mod file, but commands like go test ./… expect to be inside a module.
Proxy and GOPROXY
If you are behind a corporate firewall or network restrictions, ensure you’ve configured Go to use a proxy if needed. The GOPROXY environment variable can be set to control where Go fetches modules from.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.