ข้ามไปยังเนื้อหา

Cross-Compilation

เมื่อ deploy แอปพลิเคชัน Node.js หรือ TypeScript ไปยัง server คุณต้องมั่นใจว่าเครื่องเป้าหมายมี:

  1. Node.js version ที่ถูกต้องติดตั้งไว้
  2. node_modules/ dependencies ทั้งหมด (หรือ bundled ไว้)
  3. native addon binary ที่ถูกต้องสำหรับ target OS/CPU (เช่น bcrypt, sharp)

การ deploy ข้าม architecture — เช่น คอมไพล์บน Mac x86_64 แล้ว deploy ไป AWS Graviton server แบบ ARM — ต้องมี environment ที่ตรงกันในเครื่อง หรือ Docker build ที่ target platform ให้ถูก

Go มี support สำหรับ cross-compilation แบบ first-class คุณตั้งค่า environment variables สองตัวก่อน go build และ compiler จะสร้าง binary สำหรับ platform เป้าหมาย — ไม่ต้องใช้ Docker, cross-compiler toolchain หรือ emulation layer

Terminal window
# The two magic variables
GOOS=<target-os> GOARCH=<target-arch>
# Common values
GOOS: linux darwin windows freebsd
GOARCH: amd64 arm64 386 arm
Terminal window
# Build for Linux AMD64 (most AWS EC2, typical server)
GOOS=linux GOARCH=amd64 go build -o bin/app-linux-amd64 ./cmd/app
# Build for Linux ARM64 (AWS Graviton, Apple Silicon EC2)
GOOS=linux GOARCH=arm64 go build -o bin/app-linux-arm64 ./cmd/app
# Build for Windows from macOS or Linux
GOOS=windows GOARCH=amd64 go build -o bin/app.exe ./cmd/app
# Build for macOS Apple Silicon from any platform
GOOS=darwin GOARCH=arm64 go build -o bin/app-darwin-arm64 ./cmd/app

ลองรันคำสั่งพวกนี้ใน terminal คุณจะเห็น binary โผล่ใน bin/ ทันที cross-compiled จากเครื่อง host ของคุณโดยไม่ต้องใช้เครื่องมือเพิ่ม

Shell script ง่าย ๆ เพื่อสร้าง release binary สำหรับทุก platform ที่ใช้บ่อย:

#!/usr/bin/env bash
set -euo pipefail
APP="myapp"
VERSION=$(git describe --tags --always)
OUTDIR="dist/${VERSION}"
mkdir -p "${OUTDIR}"
targets=(
"linux/amd64"
"linux/arm64"
"darwin/amd64"
"darwin/arm64"
"windows/amd64"
)
for target in "${targets[@]}"; do
GOOS="${target%/*}"
GOARCH="${target#*/}"
ext=""
[[ "$GOOS" == "windows" ]] && ext=".exe"
output="${OUTDIR}/${APP}-${GOOS}-${GOARCH}${ext}"
echo "Building ${output}..."
CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" \
go build -ldflags="-s -w -X main.version=${VERSION}" \
-o "${output}" ./cmd/${APP}
done
echo "Done. Binaries in ${OUTDIR}/"
TypeScript
# TypeScript/Node deployment checklist
# (target machine must have all of these)
1. Install Node.js v24 on the server
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash
sudo apt-get install -y nodejs
2. Upload your build artifact + package.json
scp -r dist/ package.json server:/app/
3. Install production deps on the server
ssh server "cd /app && npm ci --omit=dev"
4. Rebuild native addons for target platform
npm rebuild bcrypt sharp # if you use them
5. Start the process
node dist/main.js
Go
# Go cross-compilation — build on dev, deploy anywhere
# Target machine needs: NOTHING. Just copy the binary.
# From your Mac, build for a Linux server:
GOOS=linux GOARCH=amd64 \
CGO_ENABLED=0 \
go build -o app-linux ./cmd/app
# Upload the single binary
scp app-linux server:/usr/local/bin/myapp
# On the server — just run it:
ssh server "/usr/local/bin/myapp"
Environment variable ไหนระบุ target operating system สำหรับ Go cross-compile?
`CGO_ENABLED=0` ทำอะไรในบริบทของ cross-compilation?
หลัง cross-compile Go binary สำหรับ Linux แล้ว Linux server เป้าหมายต้องติดตั้งอะไรเพิ่มถึงจะรัน binary นั้นได้?
ค่า GOOS ไหนที่จะใช้ build Go binary ที่รันบน Apple Silicon Mac?