找不到主模块;见 'go help modules'

Cannot find main module; see 'go help modules'

我正在构建一个 Wasm 应用程序并编译它,我有一个 shell 脚本。 当我从终端手动 运行 它时,我有以下内容:

/app/Go/assets$ ./script.compile.wasm.sh 
Wasm compiled

文件内容为:

#!/bin/sh
GOOS=js GOARCH=wasm go build -o ./app.wasm ./wasm.go
echo "Wasm compiled"

wasm 文件已正确编译。

但是当我从 Docker 运行 它时,我得到:

Step 15/20 : RUN ./assets/compile.wasm.sh
 ---> Running in 38dd56259b0f
go: cannot find main module; see 'go help modules'
Wasm compiled

编译失败

Docker 行看起来像:

RUN ./assets/compile.wasm.sh

在您的本地情况下,您从 assets 目录启动脚本;在 Dockerfile 的情况下,你从它的父目录启动它。这很重要,因为当脚本引用像 ./wasm.go 这样的文件时,这些文件是相对于当前目录而不是包含脚本的目录进行解析的。

您可以通过确保您也在 Dockerfile 的 assets 目录中来解决此问题:

# Only for this command; will reset afterwards
RUN cd assets && ./compile.wasm.sh
# For this and all following commands, unless reset with another WORKDIR
WORKDIR /app/Go/assets
RUN ./compile.wasm.sh