Deno 使用第三方代码
与外部代码连接
在前面章节中,我们看到 Deno 能够从 URL 执行脚本。
像浏览器中的 JavaScript 一样,Deno 可以从 URL 直接导入代码库。
这个示例使用 URL 来导入一个断言库:
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
assertEquals("hello", "hello");
assertEquals("world", "world");
console.log("Asserted! 🎉");
尝试运行一下:
$ deno run test.ts
Compile file:///mnt/f9/Projects/github.com/denoland/deno/docs/test.ts
Download https://deno.land/std/testing/asserts.ts
Download https://deno.land/std/fmt/colors.ts
Download https://deno.land/std/testing/diff.ts
Asserted! 🎉
对于这个程序,我们不需要提供 --allow-net
选项。
当它访问网络时,Deno 运行时有着特殊权限来下载模块并缓存到磁盘。
Deno 在一个特殊目录缓存了远程模块,该路径可以被 $DENO_DIR
指定,如果没有指定,默认为系统缓存目录。
下一次运行这个程序时无需下载。
如果这个程序没有改动,它不会被再次编译。
系统缓存目录
系统缓存目录默认为:
- Linux/Redox:
$XDG_CACHE_HOME/deno
or$HOME/.cache/deno
- Windows:
%LOCALAPPDATA%/deno
(%LOCALAPPDATA%
=FOLDERID_LocalAppData
) - macOS:
$HOME/Library/Caches/deno
如果失败,该路径设置为 $HOME/.deno
。