[Solved] Go Mod Init: Modules Disabled by GO111MODULE=off


You’ve probably just tried to initialise a new Go module, and received the following error:

go mod init: modules disabled by GO111MODULE=off; see 'go help modules'

This usually happens after running a command like:

go mod init github.com/<user>/<repo>

What is GO111MODULE?

From Go version 1.11, the way to deal with modules was revamped.

Beforehand, it was required that you place all your Go code under a $GOPATH.

Which many Go developers didn’t much like, as it seemed chaotic at best.

Also, having a package management/module management solution is so much more productive, and manageable.

Solution 1:

You could always just go and change the off to on as an environment variable and you’d be good to go!

export GO111MODULE="on"

This would result in a successful module being created after trying the previous command that resulted in the error in the first place:

go: creating new go.mod: module github.com/<user>/<repo>

If you’re on Windows, simply replace export with set.

Solution 2:

For a more direct way of achieving the same as Solution 1 above, use Go directly to update the environment variable:

go env -w GO111MODULE=on

Solution 3:

It’s quite possible that you have a go.mod file under your $GOPATH/go.mod location.

You should remove this file.

Remember that since you are using Go 1.11 or newer, you can use the go get command outside of $GOPATH, so it’s no longer required.