On this page
deno compile
标志 Jump to heading
与 deno install 一样,执行脚本时使用的运行时标志必须在编译时指定。这包括权限标志。
deno compile --allow-read --allow-net jsr:@std/http/file-server
脚本参数 可以部分嵌入。
deno compile --allow-read --allow-net jsr:@std/http/file-server -p 8080
./file_server --help
交叉编译 Jump to heading
您可以使用 --target 标志为其他平台进行交叉编译二进制文件。
# 为 Apple Silicon 进行交叉编译
deno compile --target aarch64-apple-darwin main.ts
# 为 Windows 进行交叉编译并添加图标
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts
支持的目标 Jump to heading
Deno 支持针对所有目标的交叉编译,而不管主机平台。
| 操作系统 | 架构 | 目标 |
|---|---|---|
| Windows | x86_64 | x86_64-pc-windows-msvc |
| macOS | x86_64 | x86_64-apple-darwin |
| macOS | ARM64 | aarch64-apple-darwin |
| Linux | x86_64 | x86_64-unknown-linux-gnu |
| Linux | ARM64 | aarch64-unknown-linux-gnu |
图标 Jump to heading
可以通过在目标为 Windows 时使用 --icon 标志来为可执行文件添加图标。图标必须为 .ico 格式。
deno compile --icon icon.ico main.ts
# 带有图标的交叉编译
deno compile --target x86_64-pc-windows-msvc --icon ./icon.ico main.ts
动态导入 Jump to heading
默认情况下,可静态分析的动态导入(在 import("...") 调用表达式中包含字符串字面量的导入)会被包含在输出中。
// calculator.ts 及其依赖将包含在二进制文件中
const calculator = await import("./calculator.ts");
但不可静态分析的动态导入则不会:
const specifier = condition ? "./calc.ts" : "./better_calc.ts";
const calculator = await import(specifier);
要包含不可静态分析的动态导入,请指定 --include <path> 标志。
deno compile --include calc.ts --include better_calc.ts main.ts
包含数据文件或目录 Jump to heading
从 Deno 2.1 开始,您可以通过 --include <path> 标志在可执行文件中包含文件或目录。
deno compile --include names.csv --include data main.ts
然后通过 import.meta.dirname 相对于当前模块的目录路径读取文件:
// main.ts
const names = Deno.readTextFileSync(import.meta.dirname + "/names.csv");
const dataFiles = Deno.readDirSync(import.meta.dirname + "/data");
// 在这里使用 names 和 dataFiles
请注意,这目前仅适用于文件系统上的文件,不适用于远程文件。
Worker Jump to heading
与不可静态分析的动态导入类似,默认情况下,[workers](../web_platform_apis/#web-workers) 的代码不会包含在编译后的可执行文件中。有两种方法可以包含 workers:
- 使用
--include <path>标志包含工作代码。
deno compile --include worker.ts main.ts
- 使用可静态分析的导入导入工作模块。
// main.ts
import "./worker.ts";
deno compile main.ts
自解压可执行文件 Jump to heading
默认情况下,编译后的可执行文件通过内存中的虚拟文件系统提供嵌入的文件。--self-extracting 标志改变此行为,使得二进制文件在首次运行时将所有嵌入文件解压到磁盘,并在运行时使用真实的文件系统操作。
deno compile --self-extracting main.ts
这在代码需要磁盘上的真实文件的场景中非常有用,比如本机插件或读取相对文件的本机代码。
解压目录按照优先顺序选择:
<exe_dir>/.<exe_name>/<hash>/(与编译的二进制文件相邻)- 平台数据目录备选:
- Linux:
$XDG_DATA_HOME/<exe_name>/<hash>或~/.local/share/<exe_name>/<hash> - macOS:
~/Library/Application Support/<exe_name>/<hash> - Windows:
%LOCALAPPDATA%\<exe_name>\<hash>
- Linux:
文件只解压一次 —— 后续运行如果已存在解压目录且哈希匹配,则复用该目录。
权衡 Jump to heading
自解压模式带来了更广的兼容性,但有一些权衡:
- 初始启动成本:首次运行由于文件解压耗时更长。
- 磁盘使用:解压文件占用额外磁盘空间。
- 内存使用:内存占用更高,因为嵌入内容不能再作为静态数据引用。
- 篡改风险:用户或其他代码可能修改磁盘上的解压文件。
代码签名 Jump to heading
macOS Jump to heading
默认情况下,在 macOS 上,编译后的可执行文件将使用临时签名签名,等同于运行 codesign -s -:
deno compile -o main main.ts
codesign --verify -vv ./main
./main: 磁盘上的有效
./main: 满足其指定要求
您可以在对可执行文件进行代码签名时指定签名身份,就像您对任何其他 macOS 可执行文件所做的那样:
codesign -s "Developer ID Application: Your Name" ./main
有关 macOS 上代码签名和公证的更多信息,请参考 官方文档。
Windows Jump to heading
在 Windows 上,可以使用 SignTool.exe 工具对编译后的可执行文件进行签名。
deno compile -o main.exe main.ts
signtool sign /fd SHA256 main.exe
在可执行文件中不可用 Jump to heading
Type checking options Jump to heading
--check Jump to heading
Set type-checking behavior. This subcommand type-checks local modules by default, so adding --check is redundant
If the value of "all" is supplied, remote modules will be included.
Alternatively, the 'deno check' subcommand can be used.
--no-check Jump to heading
Skip type-checking. If the value of "remote" is supplied, diagnostic errors from remote modules will be ignored.
Dependency management options Jump to heading
--cached-only Jump to heading
Require that remote dependencies are already cached.
--frozen Jump to heading
Error out if lockfile is out of date.
--import-map Jump to heading
Load import map file from local file or remote URL.
--lock Jump to heading
Check the specified lock file. (If value is not provided, defaults to "./deno.lock").
--no-lock Jump to heading
Disable auto discovery of the lock file.
--no-npm Jump to heading
Do not resolve npm modules.
--no-remote Jump to heading
Do not resolve remote modules.
--node-modules-dir Jump to heading
Sets the node modules management mode for npm packages.
--reload Jump to heading
Short flag: -r
Reload source code cache (recompile TypeScript) no value Reload everything jsr:@std/http/file-server,jsr:@std/assert/assert-equals Reloads specific modules npm: Reload all npm modules npm:chalk Reload specific npm module.
--vendor Jump to heading
Toggles local vendor folder usage for remote modules and a node_modules folder for npm packages.
Options Jump to heading
--allow-scripts Jump to heading
Allow running npm lifecycle scripts for the given packages
Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).
--cert Jump to heading
Load certificate authority from PEM encoded file.
--conditions Jump to heading
Use this argument to specify custom conditions for npm package exports. You can also use DENO_CONDITIONS env var. .
--config Jump to heading
Short flag: -c
Configure different aspects of deno including TypeScript, linting, and code formatting.
Typically the configuration file will be called deno.json or deno.jsonc and
automatically detected; in that case this flag is not necessary.
--env-file Jump to heading
Load environment variables from local file Only the first environment variable with a given key is used. Existing process environment variables are not overwritten, so if variables with the same names already exist in the environment, their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one encountered is applied. This is determined by the order of the files you pass as arguments.
--ext Jump to heading
Set content type of the supplied file.
--location Jump to heading
Value of globalThis.location used by some web APIs.
--minimum-dependency-age Jump to heading
(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable).
--no-code-cache Jump to heading
Disable V8 code cache feature.
--no-config Jump to heading
Disable automatic loading of the configuration file.
--preload Jump to heading
A list of files that will be executed before the main module.
--require Jump to heading
A list of CommonJS modules that will be executed before the main module.
--seed Jump to heading
Set the random number generator seed.
--v8-flags Jump to heading
To see a list of all available flags use --v8-flags=--help
Flags can also be set via the DENO_V8_FLAGS environment variable.
Any flags set with this flag are appended after the DENO_V8_FLAGS environment variable.
Compile options Jump to heading
--exclude Jump to heading
Excludes a file/directory in the compiled executable. Use this flag to exclude a specific file or directory within the included files. For example, to exclude a certain folder in the bundled node_modules directory.
--icon Jump to heading
Set the icon of the executable on Windows (.ico).
--include Jump to heading
Includes an additional module or file/directory in the compiled executable. Use this flag if a dynamically imported module or a web worker main module fails to load in the executable or to embed a file or directory in the executable. This flag can be passed multiple times, to include multiple additional modules.
--no-terminal Jump to heading
Hide terminal on Windows.
--output Jump to heading
Short flag: -o
Output file (defaults to $PWD/
--self-extracting Jump to heading
Create a self-extracting binary that extracts the embedded file system to disk on first run and then runs from there.
--target Jump to heading
Target OS architecture.