Skip to content

Docker 构建失败:前端 Dockerfile 使用 node:18,但 Vite 要求 Node 20.19+ #160

Description

@Zayia

问题描述

执行 docker compose up -d --build 时前端构建失败,原因是 Dockerfile 使用了 node:18-alpine
作为构建镜像,而当前版本的 Vite 要求 Node.js 20.19+ 或 22.12+。

复现步骤

git clone https://github.com/AmintaCCCP/GithubStarsManager.git
cd GithubStarsManager
docker compose up -d --build

错误信息

#30 [frontend build 6/6] RUN npm run build
#30 0.304 You are using Node.js 18.20.8. Vite requires Node.js version 20.19+ or 22.12+. Please
upgrade your Node.js version.
#30 0.309 ReferenceError: CustomEvent is not defined
#30 0.309     at CAC.parse (file:///app/node_modules/vite/dist/node/cli.js:534:28)
#30 ERROR: process "/bin/sh -c npm run build" did not complete successfully: exit code: 1

原因分析

前端 Dockerfile 第 2 行:

FROM node:18-alpine AS build

server/Dockerfile 已经使用的是 node:22-alpine,前后端构建镜像版本不一致,前端没跟上 Vite 的
Node 版本要求。

修复建议

把前端 Dockerfile 第 2 行改成:

-FROM node:18-alpine AS build
+FROM node:22-alpine AS build

这样既能满足 Vite 的 Node 版本要求,也跟 server/Dockerfile 保持一致。本地实测构建成功,约 17
秒完成,前后端容器均健康运行。

运行环境

  • Docker 27.x / Compose v2.36.2
  • 宿主机:Linux x86_64
  • 项目版本:v0.5.9(main 分支)

感谢作者开源这个很实用的工具 🙏


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions