Deno 速查表
Deno 是一个安全的 JavaScript 和 TypeScript 运行时,由 Node.js 创始人 Ryan Dahl 开发。内置 TypeScript 编译、格式化、检查、测试等功能,默认安全的权限系统,原生支持 Web API、JSR 和 npm 包。
快速开始
``bash
# 运行脚本
deno run main.ts
# 带网络权限运行 deno run --allow-net main.ts
# 监听模式——文件变更自动重启 deno run --watch main.ts
# 格式化代码 deno fmt
# 运行测试 deno test
# 启动 REPL deno repl
# 运行内联代码
deno eval 'console.log("hello deno")'
`
安装与升级
`bash
deno --version # 查看版本
deno upgrade # 升级到最新版本
deno upgrade --version 2.0.0 # 升级/降级到指定版本
deno info # 查看缓存和模块信息
`
基础使用
`bash
deno run main.ts # 运行文件
deno run --watch main.ts # 监听模式(文件变更自动重启)
deno eval 'console.log("hi")' # 运行内联代码
deno repl # 启动交互式 REPL
`
脚本管理
`bash
deno compile --allow-read main.ts # 编译为独立可执行文件
deno compile --target x86_64-unknown-linux-gnu main.ts # 交叉编译
deno compile --output dist/cli main.ts # 自定义输出名
deno install -g --allow-net --allow-read https://deno.land/std/http/file_server.ts # 安装全局脚本
deno uninstall file_server # 卸载全局脚本
deno task dev # 运行 deno.json 中定义的任务
deno task --watch test # 任务监听模式
`
权限控制
`bash
deno run --allow-read main.ts # 允许读取文件系统
deno run --allow-write main.ts # 允许写入文件系统
deno run --allow-net main.ts # 允许网络访问
deno run --allow-env main.ts # 允许环境变量访问
deno run --allow-all main.ts # 授予所有权限
deno run --allow-read=/data --allow-write=/tmp main.ts # 限定路径范围
deno run --allow-net --deny-net=api.example.com main.ts # 禁止特定主机
`
包管理
`bash
deno add @std/assert # 添加 JSR 依赖
deno add npm:express # 添加 npm 依赖
deno remove @std/assert # 移除依赖
deno publish # 发布模块到 JSR
deno cache main.ts # 缓存远程模块依赖
`
测试与检查
`bash
deno test # 运行所有测试
deno test --watch # 监听模式运行测试
deno test --coverage # 运行测试并收集覆盖率
deno bench # 运行基准测试
deno lint # 代码风格检查
deno fmt # 格式化代码(内置格式化器)
deno check main.ts # 类型检查(不执行)
`
高级功能
`bash
deno doc mod.ts # 生成文档
deno doc --json mod.ts # JSON 格式输出文档
deno jupyter --install # 启动 Jupyter kernel(实验性)
deno serve main.ts # 启动 HTTP 服务器(Fetch API)
deno run --v8-flags=--max-old-space-size=2048 main.ts # 设置 V8 内存限制
deno run --inspect-brk main.ts # 调试模式(首条语句暂停)
`
常见问题
### 如何配置 Deno?
在项目根目录创建 deno.json 或 deno.jsonc。支持任务定义、导入映射(import map)、编译选项、lint/fmt 设置和发布配置。示例:
`json
{
"tasks": {
"dev": "deno run --watch --allow-net main.ts",
"test": "deno test --coverage"
},
"imports": {
"express": "npm:express@^5.0.0"
}
}
`
### Deno 支持哪些 Web API?
Deno 实现了浏览器兼容的 Web API:fetch、Request、Response、WebSocket、Console、setTimeout、URL、Blob、File、FormData、TextEncoder、ReadableStream 等,无需 polyfill。
`typescript
// Fetch 示例(需要 --allow-net)
const res = await fetch('https://api.example.com');
const data = await res.json();
`
### 如何调试 Deno 脚本?
使用 --inspect 或 --inspect-brk 启动 V8 调试器,然后连接 Chrome DevTools 或 VS Code:
`bash
deno run --inspect-brk main.ts
# 在 Chrome 中打开 chrome://inspect
``
安装升级(4)
| 命令 | 难度 | ||
|---|---|---|---|
deno --version显示 Deno 版本和 TypeScript/V8 引擎版本 | 基础 | deno --version | |
deno upgrade升级 Deno 到最新版本 | 基础 | deno upgrade | |
deno upgrade --version 2.0.0升级/降级到指定版本 | 中级 | deno upgrade --version 2.0.0 | |
deno info显示缓存和模块加载信息 | 基础 | deno info |
基础使用(4)
| 命令 | 难度 | ||
|---|---|---|---|
deno run运行 TypeScript 或 JavaScript 文件 | 基础 | deno run main.ts | |
deno run --watch文件变更时自动重新运行 | 中级 | deno run --watch main.ts | |
deno eval运行内联代码片段 | 基础 | deno eval 'console.log("hello")' | |
deno repl启动交互式 REPL 会话 | 基础 | deno repl |
脚本管理(7)
| 命令 | 难度 | ||
|---|---|---|---|
deno compile将脚本编译为独立可执行文件 | 中级 | deno compile --allow-read main.ts | |
deno compile --target x86_64-unknown-linux-gnu交叉编译到指定目标平台 | 高级 | deno compile --target x86_64-unknown-linux-gnu main.ts | |
deno compile --output dist/cli指定输出文件名 | 中级 | deno compile --output dist/cli main.ts | |
deno install安装远程脚本为全局命令 | 中级 | deno install -g --allow-net --allow-read https://deno.land/std/http/file_server.ts | |
deno uninstall卸载已安装的全局命令 | 基础 | deno uninstall file_server | |
deno task运行 deno.json 中定义的任务 | 基础 | deno task dev | |
deno task --watch任务监听模式(文件变化自动重跑) | 中级 | deno task --watch test |
权限控制(7)
| 命令 | 难度 | ||
|---|---|---|---|
deno run --allow-read允许读取文件系统 | 基础 | deno run --allow-read main.ts | |
deno run --allow-write允许写入文件系统 | 基础 | deno run --allow-write main.ts | |
deno run --allow-net允许网络访问 | 基础 | deno run --allow-net main.ts | |
deno run --allow-all授予所有权限(不推荐生产使用) | 基础 | deno run --allow-all main.ts | |
deno run --deny-net=api.example.com拒绝特定网络地址的访问 | 高级 | deno run --allow-net --deny-net=api.example.com main.ts | |
deno run --allow-env允许环境变量访问 | 基础 | deno run --allow-env main.ts | |
deno run --allow-read=/data --allow-write=/tmp限制文件权限到指定路径 | 中级 | deno run --allow-read=/data --allow-write=/tmp main.ts |
包管理(5)
| 命令 | 难度 | ||
|---|---|---|---|
deno add添加 JSR/npm 包到项目 | 基础 | deno add @std/assert | |
deno remove从项目中移除依赖 | 基础 | deno remove @std/assert | |
deno add npm:express添加 npm 兼容包 | 基础 | deno add npm:express | |
deno publish将模块发布到 JSR | 中级 | deno publish | |
deno cache缓存远程模块依赖 | 中级 | deno cache main.ts |
测试检查(7)
| 命令 | 难度 | ||
|---|---|---|---|
deno test运行测试 | 基础 | deno test | |
deno test --watch监听模式运行测试 | 中级 | deno test --watch | |
deno test --coverage运行测试并收集覆盖率 | 中级 | deno test --coverage | |
deno bench运行基准测试 | 中级 | deno bench | |
deno lint检查代码风格和潜在错误 | 基础 | deno lint | |
deno fmt格式化代码(内置 Prettier) | 基础 | deno fmt | |
deno check对代码进行类型检查但不执行 | 中级 | deno check main.ts |
高级功能(6)
| 命令 | 难度 | ||
|---|---|---|---|
deno doc为模块生成文档 | 中级 | deno doc mod.ts | |
deno doc --json以 JSON 格式输出文档 | 中级 | deno doc --json mod.ts | |
deno jupyter启动 Jupyter Notebook kernel(实验性) | 高级 | deno jupyter --install | |
deno serve启动 HTTP 服务器(内置 Fetch API) | 高级 | deno serve main.ts | |
deno run --v8-flags=--max-old-space-size=2048设置 V8 引擎内存限制 | 高级 | deno run --v8-flags=--max-old-space-size=2048 main.ts | |
deno run --inspect-brk启用调试器(在第一条语句暂停) | 高级 | deno run --inspect-brk main.ts |