Deno简介 #
一、Deno概述 #
Deno是一个简单、现代且安全的JavaScript和TypeScript运行时,由Node.js的创始人Ryan Dahl于2018年创建。Deno基于V8 JavaScript引擎和Rust编程语言构建,旨在解决Node.js的设计缺陷,提供更安全、更简洁的开发体验。
二、Deno发展历史 #
2.1 诞生背景 #
2018年,Ryan Dahl在JSConf EU上发表演讲"关于Node.js的十件遗憾",指出了Node.js设计中的诸多问题:
- 构建系统复杂(GYP)
- package.json和node_modules的复杂性
- require语法与ES模块不兼容
- 安全性问题(任意代码可访问文件系统)
- 构建二进制文件困难
为了解决这些问题,Ryan Dahl创建了Deno项目。
2.2 主要版本演进 #
| 版本 | 发布时间 | 重要特性 |
|---|---|---|
| Deno 1.0 | 2020.05 | 首个稳定版本发布 |
| Deno 1.6 | 2020.11 | 编译为独立可执行文件 |
| Deno 1.9 | 2021.04 | 原生HTTP服务器 |
| Deno 1.20 | 2022.02 | 改进的npm兼容性 |
| Deno 1.30 | 2023.01 | 内置Node.js兼容层 |
| Deno 1.40 | 2024.01 | Temporal API支持 |
| Deno 2.0 | 2024.10 | 完整npm兼容、性能提升 |
2.3 社区认可 #
Deno获得了广泛的社区关注:
- GitHub星标超过100k
- 活跃的开源社区
- 越来越多的企业采用
三、Deno语言特点 #
3.1 安全默认 #
Deno默认禁止访问敏感资源,需要显式授权:
typescript
// 读取文件需要 --allow-read 权限
const content = await Deno.readTextFile("./hello.txt");
// 网络请求需要 --allow-net 权限
const response = await fetch("https://api.example.com/data");
运行时需要指定权限:
bash
deno run --allow-read --allow-net script.ts
3.2 TypeScript原生支持 #
Deno内置TypeScript编译器,无需配置:
typescript
interface User {
id: number;
name: string;
email: string;
}
function greet(user: User): string {
return `Hello, ${user.name}!`;
}
const user: User = {
id: 1,
name: "Alice",
email: "alice@example.com"
};
console.log(greet(user));
3.3 ES模块 #
Deno使用标准的ES模块语法,支持URL导入:
typescript
// 从URL直接导入
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
serve(() => new Response("Hello World"), { port: 8000 });
3.4 顶层await #
支持在模块顶层使用await:
typescript
// 顶层await,无需包装在async函数中
const response = await fetch("https://api.example.com/data");
const data = await response.json();
console.log(data);
3.5 Web标准API #
Deno实现了Web标准API,与浏览器环境一致:
typescript
// fetch API
const response = await fetch("https://example.com");
// WebSocket
const socket = new WebSocket("wss://example.com/socket");
// setTimeout / setInterval
setTimeout(() => console.log("Hello"), 1000);
// URL API
const url = new URL("https://example.com/path?q=test");
四、Deno的优势 #
4.1 单一可执行文件 #
Deno是一个单一的可执行文件,包含:
- 运行时
- 编译器
- 包管理器
- 代码格式化工具
- 代码检查工具
- 测试运行器
- 打包工具
无需安装额外的工具链。
4.2 无node_modules #
Deno不使用node_modules目录:
- 模块从URL加载并缓存
- 缓存存储在系统目录
- 避免依赖地狱
- 磁盘空间更小
4.3 内置工具链 #
bash
# 运行脚本
deno run script.ts
# 代码格式化
deno fmt
# 代码检查
deno lint
# 运行测试
deno test
# 编译为可执行文件
deno compile script.ts
4.4 标准库 #
Deno提供经过审核的标准库:
typescript
import { serve } from "https://deno.land/std/http/server.ts";
import { copy } from "https://deno.land/std/fs/copy.ts";
import { join } from "https://deno.land/std/path/join.ts";
4.5 Node.js兼容 #
Deno 2.0提供完整的npm包兼容:
typescript
// 直接使用npm包
import express from "npm:express@4";
const app = express();
app.get("/", (req, res) => res.send("Hello"));
app.listen(3000);
五、Deno的应用领域 #
5.1 后端服务 #
Deno非常适合构建后端服务:
typescript
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
serve((request) => {
return new Response("Hello, Deno!", {
headers: { "content-type": "text/plain" }
});
}, { port: 8000 });
5.2 CLI工具 #
开发命令行工具的理想选择:
typescript
const args = Deno.args;
const name = args[0] || "World";
console.log(`Hello, ${name}!`);
5.3 Web应用 #
使用Fresh框架构建Web应用:
typescript
// routes/index.tsx
export default function Home() {
return (
<main>
<h1>Welcome to Fresh</h1>
</main>
);
}
5.4 脚本自动化 #
替代Shell脚本:
typescript
// 构建脚本
const commands = [
"deno fmt",
"deno lint",
"deno test",
"deno compile -o app main.ts"
];
for (const cmd of commands) {
console.log(`Running: ${cmd}`);
const result = Deno.run({ cmd: cmd.split(" ") });
await result.status();
}
5.5 Serverless函数 #
Deno Deploy支持边缘部署:
typescript
// 部署到全球边缘网络
import { serve } from "https://deno.land/std/http/server.ts";
serve(() => new Response("Hello from the edge!"));
六、Deno vs Node.js #
| 特性 | Deno | Node.js |
|---|---|---|
| 安全模型 | 默认安全,需授权 | 默认无限制 |
| 模块系统 | ES模块 | CommonJS + ES模块 |
| TypeScript | 原生支持 | 需要编译 |
| 包管理 | 无package.json | npm + package.json |
| 依赖存储 | 系统缓存 | node_modules |
| 标准库 | 内置审核标准库 | 需要第三方库 |
| 顶层await | 支持 | 支持(v14.8+) |
| API设计 | Web标准API优先 | Node.js特有API |
七、Deno架构 #
7.1 核心组件 #
text
┌─────────────────────────────────────┐
│ Deno Runtime │
├─────────────────────────────────────┤
│ TypeScript Compiler (tsc) │
│ V8 JavaScript Engine │
│ Rust Core (deno_core) │
│ Tokio (Async Runtime) │
└─────────────────────────────────────┘
7.2 权限系统 #
Deno的权限系统包括:
| 权限标志 | 说明 |
|---|---|
| –allow-read | 允许读取文件系统 |
| –allow-write | 允许写入文件系统 |
| –allow-net | 允许网络访问 |
| –allow-env | 允许访问环境变量 |
| –allow-run | 允许运行子进程 |
| –allow-ffi | 允许FFI调用 |
| –allow-hrtime | 允许高精度时间 |
| -A, --allow-all | 允许所有权限 |
八、学习Deno的理由 #
- 安全第一:默认安全的运行时设计
- 现代语法:原生支持TypeScript和ES模块
- 简化开发:无配置、无node_modules
- Web标准:使用熟悉的Web API
- 工具链完整:内置格式化、检查、测试工具
- 未来趋势:边缘计算的理想选择
九、总结 #
Deno是一个现代化的JavaScript/TypeScript运行时,它:
- 提供默认安全的执行环境
- 原生支持TypeScript
- 使用标准ES模块
- 内置完整的工具链
- 与Web标准API保持一致
- 支持npm生态系统
准备好开始学习Deno了吗?让我们进入下一章,学习如何安装Deno开发环境。
最后更新:2026-03-28