Persist npm path for user services

This commit is contained in:
武阳
2026-05-07 13:00:53 +08:00
parent b72eae5da7
commit cd4e73c159
3 changed files with 37 additions and 1 deletions
+2
View File
@@ -165,6 +165,8 @@ Python 后端依赖由部署脚本安装到项目根目录 `.venv`。
前端依赖由部署脚本在 `frontend/app` 下执行 `npm ci` 安装,并执行 `npm run build` 前端依赖由部署脚本在 `frontend/app` 下执行 `npm ci` 安装,并执行 `npm run build`
部署脚本会把当前 shell 中可用的 `npm` 路径写入 `.env.deploy`,避免用户级 systemd 服务启动时读取不到 zsh/nvm 环境。
默认部署使用用户级 systemd,不需要 sudo。若机器要求服务在用户退出 SSH 后仍保持运行,可由管理员额外执行: 默认部署使用用户级 systemd,不需要 sudo。若机器要求服务在用户退出 SSH 后仍保持运行,可由管理员额外执行:
```bash ```bash
+23
View File
@@ -233,6 +233,28 @@ build_frontend() {
npm run build npm run build
} }
remember_node_runtime() {
local npm_bin node_bin node_bin_dir tmp_file
npm_bin="$(command -v npm || true)"
node_bin="$(command -v node || true)"
[[ -n "${npm_bin}" ]] || fail "无法定位 npm。"
node_bin_dir="$(dirname "${npm_bin}")"
tmp_file="$(mktemp)"
grep -v -E '^(export )?CLAW_(NPM_BIN|NODE_BIN|NODE_BIN_DIR)=' "${ENV_FILE}" >"${tmp_file}" || true
cat >>"${tmp_file}" <<EOF
# systemd 用户服务不会读取交互式 shell 配置,这里记录部署时可用的 Node.js/npm 路径。
export CLAW_NPM_BIN=$(printf "%q" "${npm_bin}")
export CLAW_NODE_BIN=$(printf "%q" "${node_bin}")
export CLAW_NODE_BIN_DIR=$(printf "%q" "${node_bin_dir}")
EOF
install -m 0600 "${tmp_file}" "${ENV_FILE}"
rm -f "${tmp_file}"
log "记录 Node.js 运行路径"
echo "npm=${npm_bin}"
echo "node=${node_bin:-未检测到}"
}
install_systemd_service() { install_systemd_service() {
local service_name="$1" local service_name="$1"
local template_path="$2" local template_path="$2"
@@ -290,6 +312,7 @@ main() {
ensure_env_file ensure_env_file
ensure_python ensure_python
build_frontend build_frontend
remember_node_runtime
install_services install_services
restart_services restart_services
health_check health_check
+12 -1
View File
@@ -20,8 +20,19 @@ BACKEND_HOST="${CLAW_BACKEND_HOST:-127.0.0.1}"
BACKEND_PORT="${CLAW_BACKEND_PORT:-8765}" BACKEND_PORT="${CLAW_BACKEND_PORT:-8765}"
FRONTEND_HOST="${CLAW_FRONTEND_HOST:-0.0.0.0}" FRONTEND_HOST="${CLAW_FRONTEND_HOST:-0.0.0.0}"
FRONTEND_PORT="${CLAW_FRONTEND_PORT:-3000}" FRONTEND_PORT="${CLAW_FRONTEND_PORT:-3000}"
NPM_BIN="${CLAW_NPM_BIN:-$(command -v npm || true)}"
if [[ -n "${CLAW_NODE_BIN_DIR:-}" ]]; then
export PATH="${CLAW_NODE_BIN_DIR}:${PATH}"
fi
if [[ -z "${NPM_BIN}" || ! -x "${NPM_BIN}" ]]; then
echo "npm not found for frontend service." >&2
echo "Run deploy again from a shell where npm is available: bash scripts/deploy-ubuntu.sh" >&2
exit 1
fi
export CLAW_API_URL="${CLAW_API_URL:-http://${BACKEND_HOST}:${BACKEND_PORT}}" export CLAW_API_URL="${CLAW_API_URL:-http://${BACKEND_HOST}:${BACKEND_PORT}}"
cd "${ROOT_DIR}/frontend/app" cd "${ROOT_DIR}/frontend/app"
exec npm run start -- --hostname "${FRONTEND_HOST}" --port "${FRONTEND_PORT}" exec "${NPM_BIN}" run start -- --hostname "${FRONTEND_HOST}" --port "${FRONTEND_PORT}"