---
title: "Vite Plus 打包 Node + TS 项目为单文件可执行程序 | Kaz"
description: "Aucun résumé pour le moment"
canonical_url: "https://unmei.fun/fr/blog/202607062210"
locale: fr
meta:
  "og:description": page.blog.202607062210.desc
  "og:image": "https://unmei.fun/favicon.svg"
  "og:image:height": "630"
  "og:image:width": "1200"
  "og:title": page.blog.202607062210.title
---

> Index of every page on this site: [llms.txt](https://unmei.fun/llms.txt)

Langues prises en charge par la page actuelle :

简体中文

# **Vite Plus 打包 Node + TS 项目为单文件可执行程序**

Nombre de caractères 2356

Temps approximatif 9 minutes

lun. 6 juillet 2026 à 14:10:00 UTC

## 背景

Node.js 从 20 版本开始提供了 SEA（Single Executable Applications）能力，可以把 JS 打包成单个可执行文件，运行时不再需要用户单独安装 Node。

整个流程分两步：先用 Vite 把 TS 项目打包成一个 CJS 文件，再用 Node 的 `--build-sea` 把它注入到 Node 可执行文件中。

项目的依赖不能超出 `SEA` 的支持范围，否则会打包失败：✅ SEA 只能稳定支持「纯 JS + 可静态打包依赖」 ❌ 一旦涉及 native addon / browser binary / runtime dynamic loader，就基本不可用

## Vite 配置

参考： [**Vite 官方文档 - ssr.noExternal**](https://vite.dev/config/ssr-options#ssr-noexternal)

vite.config.ts

```ts
import { defineConfig } from 'vite-plus'

export default defineConfig({
  ssr: {
    // 依赖内部化，避免打包后仍然需要 node_modules
    noExternal: true,
  },
  build: {
    ssr: true,
    target: 'node26',
    rolldownOptions: {
      input: 'src/main.ts',
      output: {
        format: 'cjs',
        entryFileNames: 'main.cjs',
      },
    },
  },
})
```

关键配置：

- `ssr.noExternal: true`：把所有依赖打进产物，而不是保留为外部引用；
- `build.ssr: true`：以服务端模式构建，不处理浏览器相关逻辑；
- `format: 'cjs'`：SEA 目前对 CJS 的支持更稳定；
- `target: 'node26'`：按目标 Node 版本降级语法。

## Node SEA 配置

参考： [**Node 官方文档 - Single executable applications**](https://nodejs.org/api/single-executable-applications.html)

sea-win.json

```json
{
  "main": "dist/main.cjs",
  "output": "dist/webmonitor.exe",
  "disableExperimentalSEAWarning": true,
  "useCodeCache": false,
  "useSnapshot": false
}
```

- `main`：上一步生成的入口文件；
- `output`：最终可执行文件的输出路径；
- `useCodeCache` / `useSnapshot`：开启后可以提升启动速度，但对某些代码结构兼容性较差，出问题时先关掉。

package.json

```json
{
  "type": "module",
  "scripts": {
    "dev": "node --env-file=.env src/main.ts",
    "build": "vp build",
    "build-win": "vp build && node --build-sea sea-win.json",
    "build-linux": "vp build && node --build-sea sea-linux.json"
  }
}
```

## 打包

```sh
pnpm build-win
```

执行后会在 `dist/` 下生成 `webmonitor.exe`，直接双击即可运行。

## 注意事项

- **跨平台要分别打包**：Windows 的 exe 和 Linux 的二进制不能通用，需要在对应系统上各构建一次，因此脚本里区分了 `build-win` 和 `build-linux`。
- **运行时资源**：通过 `fs` 读取的配置文件、模板等不会自动打进可执行文件，需要以 SEA 资源的方式注入，或改为随程序分发。
- **原生依赖**： `better-sqlite3`、 `sharp` 这类包含 `.node` 文件的依赖无法被静态打包，遇到相关报错时应优先考虑更换实现。
- **调试**：SEA 模式下报错堆栈的行号可能对不上源码，排查问题时可以先运行 `node dist/main.cjs` 验证逻辑。
- **体积**：产物包含 Node 运行时，体积通常在几十 MB，属于正常现象。

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://unmei.fun/fr#website","@type":"WebSite","inLanguage":"fr","name":"HomePage","url":"https://unmei.fun/fr","translationOfWork":{"@id":"https://unmei.fun/#website"}},{"@id":"https://unmei.fun/fr/blog/202607062210#webpage","@type":"WebPage","description":"Aucun résumé pour le moment","name":"Vite Plus 打包 Node + TS 项目为单文件可执行程序","url":"https://unmei.fun/fr/blog/202607062210","breadcrumb":{"@id":"https://unmei.fun/fr/blog/202607062210#/schema/breadcrumb-list/#breadcrumb"},"isPartOf":{"@id":"https://unmei.fun/fr#website"},"potentialAction":[{"@type":"ReadAction","target":["https://unmei.fun/fr/blog/202607062210"]}]},{"@id":"https://unmei.fun/fr/blog/202607062210#/schema/breadcrumb-list/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Home","item":"https://unmei.fun/fr","position":1},{"@type":"ListItem","name":"Blog","item":"https://unmei.fun/fr/blog","position":2},{"@type":"ListItem","name":"202607062210","item":"https://unmei.fun/fr/blog/202607062210","position":3}]}]}
```

## Sitemap

See the full [sitemap](https://unmei.fun/sitemap.md) for all pages.
