Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

go

I just updated to the new version of Go - Go version 1.16.2 Linux/amd64 and am getting an error when I build a Hello, World! example:

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

Even when I follow the fix from that post it isn't working. I set these variables and then build again:

GO111MODULE=on GOPROXY=https://proxy.golang.org,direct 

And the same problem unfortunately.

like image 311
shwick Avatar asked Mar 31 '21 19:03

shwick


People also ask

Where does go mod file go?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.

What is go mod in Golang?

The go. mod file defines the module's module path, which is also the import path used for the root directory, and its dependency requirements, which are the other modules needed for a successful build. Each dependency requirement is written as a module path and a specific semantic version.

How do I run a go module?

Open a command prompt and cd to your home directory. Create a greetings directory for your Go module source code. Start your module using the go mod init command. Run the go mod init command, giving it your module path -- here, use example.com/greetings .

Where are go module dependencies installed?

mod module config file in your project's root directory. The file will contain the module and Go version information. The file stipulates the project's requirements and lists all the needed dependencies—it's like the package.

What is the go MOD file?

go: go.mod file not found in current directory or any parent directory; see 'go help modules' Go module is the official go dependency management library of go, which is officially recommended in version 1.13 Go module can organize all dependencies under a project (folder) into one go Mod file, in which the dependent version is written

Is it possible to build packages in Gopath mode?

It’s still possible to build packages in GOPATH mode by setting the GO111MODULE environment variable to off . You can also set GO111MODULE to auto to enable module-aware mode only when a go.mod file is present in the current directory or any parent directory.

Why doesn’t the GO command automatically fix imported packages?

The automatic fixes weren’t always desirable: if an imported package wasn’t provided by any required module, the go command would add a new dependency, possibly triggering upgrades of common dependencies. Even a misspelled import path would result in a (failed) network lookup.

Does the GO command automatically fix the missing Requirement Directive?

Previously, when the go command found a problem with go.mod or go.sum like a missing require directive or a missing sum, it would attempt to fix the problem automatically. We received a lot of feedback that this behavior was surprising, especially for commands like go list that don’t normally have side effects.


1 Answers

Change this:

go env -w GO111MODULE=auto

to this:

go env -w GO111MODULE=off

like image 157
Chris Byron Avatar answered Sep 19 '22 16:09

Chris Byron