Skip to content

/errors/go/go-mod-file-not-found

go: go.mod file not found in current directory or any parent directory; see 'go help modules'

The commands on this page have not been executed by our verification runner yet.updated
tool
Go
exit code
1
versions
Go 1.16+ (module mode is the default)
os
any

go: go.mod file not found in current directory or any parent directory; see 'go help modules' means the go command is in module mode (the default since Go 1.16) and searched the current directory and every parent for a go.mod file without finding one. Commands that build or resolve packages (go build, go run ./..., go test, go get, go mod tidy) need a module. Run go mod init with your module path in the project root, or cd into the directory that already contains go.mod.

What it looks like

The message comes from the go command itself (cmd/go/internal/modload), before any compilation:

text
$ go build ./...
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

A related message appears when the directory has no go.mod but contains a .git (or other VCS) directory, in which case go suggests where to create the module:

text
go: cannot find main module, but found .git/config in /path/to/project
	to create a module there, run:
	go mod init

In workspace mode (a go.work file is in effect) the wording is no modules were found in the current workspace; see 'go help work'.

Fix: create the module

bash
cd /path/to/project
go mod init example.com/myapp     # module path, usually the repository URL
go mod tidy                       # adds requirements for the imports in your code
go build ./...

The module path is a name, not a location: github.com/you/myapp if the code will be imported by others, any name such as myapp for a local program.

Installing a tool

Since Go 1.17, go get only manages dependencies of the current module and no longer builds binaries. Use go install with a version, which does not need a go.mod:

bash
go install golang.org/x/tools/cmd/goimports@latest

Running from the wrong directory

SituationFix
Terminal opened in the parent foldercd into the folder that contains go.mod
CI job (GitHub Actions, GitLab)set working-directory to the module root
Editor (gopls) opened on a folder above several modulesopen the module folder, or create a go.work with go work init ./mod1 ./mod2
Old GOPATH projectrun go mod init in the project root to convert it

Sources