运行时与命令 #
一、CLI概述 #
Deno提供了一个功能完整的命令行工具,包含运行、测试、格式化、检查、编译等功能。本章将详细介绍Deno CLI的各种命令。
二、基本命令 #
2.1 查看帮助 #
bash
# 查看主帮助
deno --help
# 查看子命令帮助
deno run --help
deno test --help
2.2 查看版本 #
bash
deno --version
输出:
text
deno 2.0.0 (release, aarch64-apple-darwin)
v8 12.1.285.6
typescript 5.3.3
2.3 执行代码 #
bash
# 执行代码字符串
deno eval "console.log('Hello, Deno!')"
# 执行TypeScript代码
deno eval --ext=ts "const x: number = 42; console.log(x)"
三、deno run - 运行脚本 #
3.1 基本用法 #
bash
# 运行本地文件
deno run script.ts
# 运行远程文件
deno run https://deno.land/std@0.208.0/examples/welcome.ts
3.2 权限选项 #
bash
# 允许所有权限
deno run -A script.ts
# 允许网络访问
deno run --allow-net script.ts
# 允许特定域名
deno run --allow-net=api.example.com,cdn.example.com script.ts
# 允许文件读取
deno run --allow-read script.ts
# 允许读取特定目录
deno run --allow-read=/tmp,./data script.ts
# 允许文件写入
deno run --allow-write script.ts
# 允许环境变量
deno run --allow-env script.ts
# 允许运行子进程
deno run --allow-run script.ts
# 组合权限
deno run --allow-net --allow-read --allow-env script.ts
3.3 其他选项 #
bash
# 监听文件变化自动重启
deno run --watch script.ts
# 监听所有文件变化
deno run --watch=all script.ts
# 只检查类型,不执行
deno run --check script.ts
# 不检查类型
deno run --no-check script.ts
# 指定配置文件
deno run --config deno.json script.ts
# 使用导入映射
deno run --import-map import_map.json script.ts
# 锁定依赖版本
deno run --lock deno.lock script.ts
四、deno test - 测试 #
4.1 基本用法 #
bash
# 运行所有测试
deno test
# 运行特定文件
deno test test.ts
# 运行匹配名称的测试
deno test --filter "user"
# 运行特定目录的测试
deno test src/
4.2 测试选项 #
bash
# 允许所有权限
deno test -A
# 显示测试覆盖率
deno test --coverage=coverage
# 并行运行测试
deno test --parallel
# 允许测试中的操作
deno test --allow-read --allow-write
# 详细输出
deno test --trace-leaks
# 报告格式
deno test --reporter=dot
deno test --reporter=pretty
deno test --reporter=junit
4.3 测试示例 #
typescript
import { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";
Deno.test("简单测试", () => {
assertEquals(1 + 1, 2);
});
Deno.test("异步测试", async () => {
const response = await fetch("https://example.com");
assertEquals(response.status, 200);
});
Deno.test({
name: "带选项的测试",
ignore: Deno.build.os === "windows",
fn: () => {
assertEquals(true, true);
}
});
五、deno fmt - 代码格式化 #
5.1 基本用法 #
bash
# 格式化当前目录所有文件
deno fmt
# 格式化特定文件
deno fmt script.ts
# 格式化特定目录
deno fmt src/
# 检查格式但不修改
deno fmt --check
5.2 格式化选项 #
bash
# 使用制表符缩进
deno fmt --options-use-tabs
# 设置行宽
deno fmt --options-line-width=100
# 设置缩进宽度
deno fmt --options-indent-width=4
# 不使用分号
deno fmt --options-no-semicolons
# 单引号字符串
deno fmt --options-single-quote
5.3 配置文件 #
在 deno.json 中配置:
json
{
"fmt": {
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"semiColons": true,
"singleQuote": false,
"include": ["src/"],
"exclude": ["src/generated/"]
}
}
5.4 忽略文件 #
使用注释忽略格式化:
typescript
// deno-fmt-ignore
const data = {
"key1": "value1",
"key2": "value2",
};
忽略整个文件:
typescript
// deno-fmt-ignore-file
六、deno lint - 代码检查 #
6.1 基本用法 #
bash
# 检查当前目录
deno lint
# 检查特定文件
deno lint script.ts
# 检查特定目录
deno lint src/
# 输出JSON格式
deno lint --json
6.2 规则配置 #
在 deno.json 中配置:
json
{
"lint": {
"include": ["src/"],
"exclude": ["src/generated/"],
"rules": {
"exclude": ["no-explicit-any", "no-unused-vars"]
}
}
}
6.3 常用规则 #
| 规则 | 说明 |
|---|---|
| no-explicit-any | 禁止使用any类型 |
| no-unused-vars | 禁止未使用的变量 |
| no-inferrable-types | 禁止可推断的类型注解 |
| prefer-const | 优先使用const |
| require-await | async函数必须包含await |
| no-empty | 禁止空代码块 |
6.4 忽略检查 #
typescript
// deno-lint-ignore no-explicit-any
const data: any = {};
// 忽略多个规则
// deno-lint-ignore no-explicit-any no-unused-vars
const x: any = 1;
忽略整个文件:
typescript
// deno-lint-ignore-file
七、deno compile - 编译 #
7.1 基本用法 #
bash
# 编译为可执行文件
deno compile script.ts
# 指定输出文件名
deno compile --output my-app script.ts
# 编译带权限
deno compile --allow-net --allow-read --output my-app script.ts
# 允许所有权限
deno compile -A --output my-app script.ts
7.2 编译选项 #
bash
# 指定目标平台
deno compile --target x86_64-pc-windows-msvc script.ts
deno compile --target x86_64-apple-darwin script.ts
deno compile --target aarch64-apple-darwin script.ts
deno compile --target x86_64-unknown-linux-gnu script.ts
# 包含图标(Windows)
deno compile --icon icon.ico script.ts
# 优化大小
deno compile --no-check --output my-app script.ts
7.3 编译示例 #
typescript
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
serve(() => new Response("Hello World"), { port: 8000 });
编译:
bash
deno compile --allow-net --output server server.ts
八、deno cache - 缓存管理 #
8.1 基本用法 #
bash
# 缓存依赖
deno cache script.ts
# 重新加载所有依赖
deno cache --reload script.ts
# 重新加载特定模块
deno cache --reload=https://deno.land/std script.ts
# 生成锁定文件
deno cache --lock=deno.lock --lock-write script.ts
# 使用锁定文件
deno cache --lock=deno.lock script.ts
8.2 缓存位置 #
默认缓存位置:
- macOS/Linux:
~/.cache/deno - Windows:
%LOCALAPPDATA%\deno
可通过环境变量修改:
bash
export DENO_DIR=/custom/cache/path
九、deno task - 任务运行 #
9.1 定义任务 #
在 deno.json 中定义:
json
{
"tasks": {
"dev": "deno run --watch main.ts",
"start": "deno run --allow-net main.ts",
"test": "deno test --allow-all",
"build": "deno compile --output app main.ts",
"lint": "deno lint",
"fmt": "deno fmt",
"check": "deno check main.ts"
}
}
9.2 运行任务 #
bash
# 列出所有任务
deno task
# 运行特定任务
deno task dev
deno task test
deno task build
9.3 任务组合 #
json
{
"tasks": {
"check": "deno fmt --check && deno lint && deno check main.ts",
"ci": "deno task check && deno task test"
}
}
十、deno info - 模块信息 #
10.1 基本用法 #
bash
# 查看模块信息
deno info script.ts
# 查看远程模块信息
deno info https://deno.land/std@0.208.0/http/server.ts
# 查看缓存位置
deno info --location
10.2 输出示例 #
text
local: /Users/user/project/script.ts
type: TypeScript
dependencies: 2 unique
size: 15.2KB
https://deno.land/std@0.208.0/http/server.ts (10.5KB)
└── https://deno.land/std@0.208.0/http/_mock_conn.ts (1.2KB)
十一、deno doc - 文档生成 #
11.1 基本用法 #
bash
# 生成文档
deno doc script.ts
# 生成JSON格式
deno doc --json script.ts
# 生成HTML文档
deno doc --html --output=docs script.ts
11.2 文档注释 #
typescript
/**
* 计算两个数的和
*
* @param a - 第一个数
* @param b - 第二个数
* @returns 两数之和
*
* @example
* ```ts
* const result = add(1, 2); // 3
* ```
*/
export function add(a: number, b: number): number {
return a + b;
}
十二、deno repl - 交互式环境 #
12.1 基本用法 #
bash
# 启动REPL
deno
# 或
deno repl
# 带权限启动
deno repl --allow-read
12.2 REPL命令 #
text
> 1 + 1
2
> const x = await Deno.readTextFile("./hello.ts")
undefined
> console.log(x)
console.log("Hello, Deno!");
undefined
> .exit
12.3 REPL快捷键 #
| 快捷键 | 功能 |
|---|---|
| Ctrl+C | 取消当前输入 |
| Ctrl+D | 退出REPL |
| Tab | 自动补全 |
| ↑/↓ | 历史命令 |
十三、deno install - 安装脚本 #
13.1 安装脚本 #
bash
# 安装脚本为全局命令
deno install --allow-net --allow-read -n my-tool script.ts
# 安装远程脚本
deno install -n serve https://deno.land/std@0.208.0/http/file_server.ts
13.2 安装选项 #
bash
# 指定安装目录
deno install --root ~/.local/bin script.ts
# 安装位置
# 默认: ~/.deno/bin
13.3 管理已安装脚本 #
bash
# 列出已安装脚本
ls ~/.deno/bin
# 删除已安装脚本
rm ~/.deno/bin/my-tool
十四、deno uninstall - 卸载 #
bash
# 卸载已安装的脚本
deno uninstall my-tool
十五、deno upgrade - 升级 #
bash
# 升级到最新版本
deno upgrade
# 升级到指定版本
deno upgrade 2.0.0
# 升级到预览版本
deno upgrade --canary
deno upgrade --rc
# 指定版本
deno upgrade --version 2.0.0
十六、权限标志汇总 #
| 标志 | 说明 | 示例 |
|---|---|---|
| -A, --allow-all | 允许所有权限 | -A |
| –allow-read | 允许读取 | –allow-read=/tmp |
| –allow-write | 允许写入 | –allow-write=./out |
| –allow-net | 允许网络 | –allow-net=api.com |
| –allow-env | 允许环境变量 | –allow-env=HOME |
| –allow-run | 允许运行子进程 | –allow-run=git |
| –allow-ffi | 允许FFI | –allow-ffi |
| –allow-hrtime | 允许高精度时间 | –allow-hrtime |
| –prompt | 权限请求时提示 | –prompt |
| –no-prompt | 禁用权限提示 | –no-prompt |
十七、总结 #
本章学习了:
- deno run 运行脚本
- deno test 运行测试
- deno fmt 格式化代码
- deno lint 代码检查
- deno compile 编译可执行文件
- deno cache 缓存管理
- deno task 任务运行
- deno info 模块信息
- deno doc 文档生成
- deno repl 交互式环境
- deno install 安装脚本
- deno upgrade 升级版本
- 权限标志的使用
下一章,我们将学习Deno的数据类型。
最后更新:2026-03-28