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
+23
View File
@@ -233,6 +233,28 @@ build_frontend() {
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() {
local service_name="$1"
local template_path="$2"
@@ -290,6 +312,7 @@ main() {
ensure_env_file
ensure_python
build_frontend
remember_node_runtime
install_services
restart_services
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}"
FRONTEND_HOST="${CLAW_FRONTEND_HOST:-0.0.0.0}"
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}}"
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}"