gogo-modulesgo-build

Error: "build flag -mod=vendor only valid when using modules" when building Go project


According to this document I need to add -mod=vendor to my build command to make use of my local vendor folder:

By default, go commands like go build ignore the vendor directory when in module mode. The -mod=vendor flag (e.g., go build -mod=vendor) instructs the go commands to use the main module's top-level vendor directory to satisfy dependencies.

When I run this command:

go build -mod=vendor -a -ldflags "-s -w -X github.com/my-api/pkg/config.Version=169.3988801" -o bin/my-api

I get this error:

build flag -mod=vendor only valid when using modules

Locally the command works as expected, the error only occurs on the build server.


Solution

  • I think the key part of the help you are showing is when in module mode. This will only happen when you are working on code outside of GOPATH or if you have set the GO111MODULE=on environment variable so I think you have this set in your local environment but not the build server.

    More information about enabling module support is here:

    https://golang.org/cmd/go/#hdr-Preliminary_module_support