URL导入 #

一、URL导入概述 #

Deno的一大特色是支持直接从URL导入模块,无需包管理器和中央仓库。这种方式简化了依赖管理,实现了真正的去中心化。

二、基本用法 #

2.1 从deno.land导入 #

typescript
// 导入标准库
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";
import { readCSV } from "https://deno.land/std@0.208.0/csv/mod.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";

// 使用serve
serve(() => new Response("Hello World"), { port: 8000 });

2.2 从GitHub导入 #

typescript
// 从GitHub仓库导入
import { someModule } from "https://raw.githubusercontent.com/user/repo/main/mod.ts";

// 从特定分支导入
import { feature } from "https://raw.githubusercontent.com/user/repo/feature-branch/mod.ts";

// 从特定标签导入
import { stable } from "https://raw.githubusercontent.com/user/repo/v1.0.0/mod.ts";

2.3 从CDN导入 #

typescript
// Skypack CDN
import lodash from "https://cdn.skypack.dev/lodash@4.17.21";
import React from "https://cdn.skypack.dev/react@18.2.0";

// esm.sh CDN
import lodash from "https://esm.sh/lodash@4.17.21";
import React from "https://esm.sh/react@18.2.0";

// unpkg CDN
import lodash from "https://unpkg.com/lodash@4.17.21/lodash.js";

2.4 npm包导入 #

typescript
// 使用npm:前缀
import express from "npm:express@4.18.2";
import { z } from "npm:zod@3.22.4";
import chalk from "npm:chalk@5.3.0";

const app = express();
const schema = z.object({ name: z.string() });
console.log(chalk.blue("Hello"));

2.5 jsr包导入 #

typescript
// 使用jsr:前缀
import { encodeBase64 } from "jsr:@std/encoding/base64";
import { join } from "jsr:@std/path/join";

const encoded = encodeBase64(new TextEncoder().encode("hello"));
const path = join("src", "utils", "mod.ts");

三、版本管理 #

3.1 固定版本 #

typescript
// 固定版本号
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

// 使用语义化版本范围
import { serve } from "https://deno.land/std@0.208/http/server.ts";

3.2 使用最新版本 #

typescript
// 不推荐:使用最新版本可能导致不稳定
import { serve } from "https://deno.land/std/http/server.ts";

// 推荐:固定版本
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

3.3 锁定文件 #

bash
# 生成锁定文件
deno cache --lock=deno.lock --lock-write main.ts

# 使用锁定文件验证
deno cache --lock=deno.lock main.ts

锁定文件示例:

json
{
  "version": "3",
  "packages": {
    "specifiers": {
      "https://deno.land/std@0.208.0/http/server.ts": "..."
    }
  }
}

四、缓存机制 #

4.1 缓存位置 #

bash
# 查看缓存位置
deno info --location

# macOS/Linux: ~/.cache/deno
# Windows: %LOCALAPPDATA%\deno

4.2 缓存命令 #

bash
# 缓存依赖
deno cache main.ts

# 重新加载所有依赖
deno cache --reload main.ts

# 重新加载特定模块
deno cache --reload=https://deno.land/std main.ts

# 清除缓存
rm -rf ~/.cache/deno

4.3 查看模块信息 #

bash
# 查看模块信息
deno info https://deno.land/std@0.208.0/http/server.ts

# 查看依赖树
deno info main.ts

五、依赖管理 #

5.1 deps.ts模式 #

typescript
// deps.ts - 集中管理依赖
export { serve } from "https://deno.land/std@0.208.0/http/server.ts";
export { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";
export { readCSV } from "https://deno.land/std@0.208.0/csv/mod.ts";

// 使用时
import { serve, assertEquals } from "./deps.ts";

5.2 导入映射 #

json
// import_map.json
{
  "imports": {
    "@std/http": "https://deno.land/std@0.208.0/http",
    "@std/testing": "https://deno.land/std@0.208.0/testing",
    "lodash": "https://cdn.skypack.dev/lodash@4.17.21"
  }
}
typescript
// 使用映射
import { serve } from "@std/http/server.ts";
import { assertEquals } from "@std/testing/asserts.ts";
import _ from "lodash";

5.3 deno.json配置 #

json
{
  "imports": {
    "@std/http": "jsr:@std/http@^1.0.0",
    "@std/fs": "jsr:@std/fs@^1.0.0",
    "lodash": "npm:lodash@^4.17.21"
  }
}

六、私有模块 #

6.1 私有仓库 #

typescript
// 从私有GitHub仓库导入(需要认证)
import { secret } from "https://raw.githubusercontent.com/user/private-repo/main/mod.ts";

// 使用环境变量存储token
const token = Deno.env.get("GITHUB_TOKEN");
const url = `https://${token}@raw.githubusercontent.com/user/private-repo/main/mod.ts`;

6.2 私有服务器 #

typescript
// 从私有服务器导入
import { api } from "https://internal.company.com/modules/api.ts";

七、安全性 #

7.1 校验模块 #

bash
# 查看模块内容
deno eval "import 'https://deno.land/std@0.208.0/http/server.ts'"

# 检查模块信息
deno info https://deno.land/std@0.208.0/http/server.ts

7.2 使用锁定文件 #

bash
# 生成锁定文件
deno cache --lock=deno.lock --lock-write main.ts

# CI中验证
deno cache --lock=deno.lock main.ts

7.3 限制网络权限 #

bash
# 只允许特定域名
deno run --allow-net=deno.land,api.example.com main.ts

八、最佳实践 #

8.1 固定版本 #

typescript
// 推荐:固定版本
import { serve } from "https://deno.land/std@0.208.0/http/server.ts";

// 不推荐:不固定版本
import { serve } from "https://deno.land/std/http/server.ts";

8.2 使用deps.ts #

typescript
// deps.ts
export { serve } from "https://deno.land/std@0.208.0/http/server.ts";
export { assertEquals } from "https://deno.land/std@0.208.0/testing/asserts.ts";

// 其他文件
import { serve, assertEquals } from "./deps.ts";

8.3 使用导入映射 #

json
{
  "imports": {
    "@std/": "https://deno.land/std@0.208.0/"
  }
}

8.4 使用锁定文件 #

bash
# 提交锁定文件到版本控制
git add deno.lock

九、总结 #

本章学习了:

  • URL导入的基本用法
  • 各种模块源(deno.land、GitHub、CDN、npm、jsr)
  • 版本管理
  • 缓存机制
  • 依赖管理模式
  • 私有模块处理
  • 安全性考虑
  • 最佳实践

下一章,我们将学习导入映射。

最后更新:2026-03-28