200

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shwick
  • 4,277
  • 6
  • 21
  • 28
  • 36
    Did you run `go mod init` as shown in [How to Write Go Code](https://golang.org/doc/code), [Getting Started](https://golang.org/doc/tutorial/getting-started), or [Creating a Module](https://golang.org/doc/tutorial/create-module)? – JimB Mar 31 '21 at 19:35
  • 5
    Read and stick to https://golang.org/doc/#getting-started. You must use modules in Go 1.16. – Volker Mar 31 '21 at 19:36
  • 1
    In what context are *GO111MODULE* and *GOPROXY* set? Inside file *go.mod*? Or somewhere else? – Peter Mortensen Feb 02 '22 at 22:57
  • @PeterMortensen It should be set in your shell. It could be in a shells script too. – Alexis Wilke Apr 27 '22 at 17:51

15 Answers15

267

Change this:

go env -w GO111MODULE=auto

to this:

go env -w GO111MODULE=off

Vlad Havriuk
  • 1,291
  • 17
  • 29
Chris Byron
  • 2,679
  • 1
  • 5
  • 3
  • 13
    it worked man thanks but can you explain why it works ? – Bhumit 070 Jul 21 '21 at 17:12
  • this worked. thanks.. should be included in the documentation on github – Parv Sharma Jul 23 '21 at 18:35
  • 8
    GO111MODULE=off forces Go to behave the GOPATH way, even outside of GOPATH – Ankit Gupta Oct 13 '21 at 11:28
  • Read more about it here : https://maelvls.dev/go111module-everywhere/ – Ankit Gupta Oct 13 '21 at 11:28
  • 1
    this really works – junnyea Nov 03 '21 at 03:03
  • This should be the answer, it is a one-liner, works, and is very clear on what it says to do. – Sandeep Anand Dec 29 '21 at 04:25
  • 13
    Why does this work? What is it supposed to do? Are there some security implications of this change? What is the theory of operation? – Peter Mortensen Feb 02 '22 at 21:56
  • 1
    First make sure that your GO111MODULE value is set to "auto". You can check it from: ```bash go env ``` if it is not set to "auto", run: ```bash go env -w GO111MODULE=auto ``` go to your work directory in terminal and run: ```bash go mod init 'project_name' go mod tidy ``` set to "off" again, or else your code won't run, to do so, run: ```bash go env -w GO111MODULE=off ``` You are good to Go! – ZamaaN May 29 '22 at 09:28
141

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shwick
  • 4,277
  • 6
  • 21
  • 28
82

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ch9xy
  • 920
  • 4
  • 6
20

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Walexhino
  • 199
  • 1
  • 6
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SparkZeus
  • 338
  • 3
  • 5
12

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"]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    [Docker](https://en.wikipedia.org/wiki/Docker_%28software%29)-related, presumably? How does that answer the question? What is "`EXPOSE 6111`" supposed to do? Why do `GO111MODULE` and `GOPROXY` not appear in this answer? What version of Docker and which host system (incl. version) was this tested on? Can you [elaborate](https://stackoverflow.com/posts/68158579/edit) in your answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Feb 02 '22 at 23:14
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcel
  • 139
  • 2
  • 6
3

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vineesh P
  • 53
  • 5
3
  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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Siwei
  • 19,858
  • 7
  • 75
  • 95
2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dmotta
  • 1,843
  • 2
  • 21
  • 32
2

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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
erikric
  • 4,049
  • 8
  • 32
  • 44
1

just go to the parent directory and run go mod init "child/project root directory name"

Farshad
  • 337
  • 2
  • 9
1

I am using go v1.18. My problem was go work init. See here for more detail:
VScode show me the error after I install the proxy in vscode

I had to do:

$ cd /my/parent/dir
$ go work init
$ go work use project-one
$ go work use project-two

Now I can do:

$ cd {root directory of the module where go.mod is}
$ cd ./project-one
$ go build .
$ go run .
lechat
  • 390
  • 2
  • 15
0

enter code here FROM golang:1.20.5-alpine

WORKDIR /app

COPY main.go .

RUN go mod init main
RUN go build -o test.go

CMD ["./test.go"]

Change from RUN go mod init to RUN go mod init main else you will get

go: cannot determine module path for source directory /app (outside GOPATH, module path must be specified)

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
0

for me it just remove or disable zsh-golang plugin. https://github.com/wintermi/zsh-golang

Suhendra
  • 91
  • 1
  • 3