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

LinuxGo

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

Change this:

go env -w GO111MODULE=auto

to this:

go env -w GO111MODULE=off

Solution 2 - Linux

Yes, just follow the tutorial and for me that was doing go mod init test3 to create a module. No one else has been upgrading from the old version or everyone else just understood it properly I guess.

Solution 3 - Linux

First make sure that your GO111MODULE value is set to "auto". You can check it from:

go env

If it is not set to "auto", then run:

go env -w GO111MODULE=auto

Go to your work directory in terminal and run:

go mod init
go mod tidy

You are good to go!

Solution 4 - Linux

From your project directory, add this line of code in your Dockerfile file if you are building a Docker image:

RUN go mod init

Or this in your directory:

go mod init

Solution 5 - Linux

This worked for me:

FROM golang:alpine

WORKDIR /go/src/app

ADD . .
RUN go mod init

RUN go build  -o /helloworld

EXPOSE 6111

CMD ["./helloworld"]

Solution 6 - Linux

Go builds packages in module-aware mode by default starting from 1.16. See here.

Navigate to the folder containing the Go file and run:

go mod init <modulename>.

A go.mod file is created here and this directory will become the root of the newly created module. See here to read about Go modules.

Solution 7 - Linux

I was trying to run the go build command on my Windows local machine and I ran into the go: go.mod file not found in current directory or any parent directory; see 'go help modules' error.

This is how I went about solving it:

  • First I got the root directory of my application by running $ pwd and got a response like so /c/projects/go-projects/go-server
  • Then I ran $ go mod init c/projects/go-projects/go-server

This totally took away the error and I was able to run the server with the command $ ./go-web

PS: It is important to note that I was running Linux commands on my machine using the Git Bash Linux terminal for Windows, it comes by default in Windows with the installation of git for windows.

Solution 8 - Linux

Try running the below commands

  • 'go mod init example.com/m' to initialize a v0 or v1 module
  • 'go mod init example.com/m/v2' to initialize a v2 module

Solution 9 - Linux

I had the same error when I build a Docker image:

docker build . -t ms-c3alert:1.0.6

Error detail:

Step 11/21 : ENV GO111MODULE=on

 ---> Using cache
 ---> 1f1728be0f4a
Step 12/21 : RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o c3alert .
 ---> Running in ee4b3d33d1d2
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
The command '/bin/sh -c CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o c3alert .' returned a non-zero code: 1

I've resolved it by adding the following lines in my Dockerfile. Previously, it required to have a Go project in modules, according the guide in previous posts:

COPY go.mod .
COPY go.sum .
RUN go mod download

Solution 10 - Linux

I recently discovered that the "no required module" is the error message you get when trying to run/build a Go-file that doesn't exist or has a typo.

My student had named his file 'filename.Go'. The capital 'G' made it an invalid Go file, but Visual Studio Code still recognized it as a Go file, so it took a long time to discover it.

I spent a loooong time figuring this out, trying to understand modules and the relationship to folders, when the problem was a typo.

A confusing error message when it was actually the Go file, and not the .mod file that was "missing".

Solution 11 - Linux

  1. Do not install Go in your home folder. E.g. ~/go, because this folder will be used by To in further third party libraries' installation

  2. download and unzip the Go install ZIP file to another place, e.g. /workspace/tools/go and set your PATH=/workspace/tools/go/bin

That's it. There isn't any need to do go mod init.

Solution 12 - Linux

I have just started learning Go.

And I have found this if you build your programme like below:

go build "name of go app with path"

go build .\Main.go

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshwickView Question on Stackoverflow
Solution 1 - LinuxChris ByronView Answer on Stackoverflow
Solution 2 - LinuxshwickView Answer on Stackoverflow
Solution 3 - Linuxch9xyView Answer on Stackoverflow
Solution 4 - LinuxWalexhinoView Answer on Stackoverflow
Solution 5 - LinuxJoseph Mulingwa KithomeView Answer on Stackoverflow
Solution 6 - LinuxSparkZeusView Answer on Stackoverflow
Solution 7 - LinuxMarcelView Answer on Stackoverflow
Solution 8 - LinuxVineesh PView Answer on Stackoverflow
Solution 9 - LinuxdmottaView Answer on Stackoverflow
Solution 10 - LinuxerikricView Answer on Stackoverflow
Solution 11 - LinuxSiweiView Answer on Stackoverflow
Solution 12 - LinuxAnkit PatankarView Answer on Stackoverflow