On this page
标准库 (@std)
Deno 提供了一个用 TypeScript 编写的标准库。它是一组可被程序重用的标准模块,让你专注于应用逻辑,而不是为常见任务“重新发明轮子”。Deno 标准库中的所有模块都经过核心团队审查,保证能够与 Deno 兼容,确保一致性和可靠性。
Deno 标准库中的许多包也兼容 Node.js、Cloudflare Workers 以及其他 JavaScript 环境。这让你可以编写无需修改即可在多个环境中运行的代码。
标准库托管在 JSR 上,地址为:https://jsr.io/@std。包都有文档、测试,并且包含用法示例。
包 Jump to heading
- @std/assert – Common assertion functions, especially useful for testing
- @std/async – Utilities for asynchronous operations, like delays, debouncing, or pooling
- @std/bytes – Utilities to manipulate Uint8Arrays that are not built-in to JavaScript
- @std/cache – UNSTABLE: Cache utilities
- @std/cbor – UNSTABLE: Utilities for parsing and serializing Concise Binary Object Representation (CBOR)
- @std/cli – Tools for creating interactive command line tools
- @std/collections – Pure functions for common tasks related to collection types like arrays and objects
- @std/crypto – Extensions to the Web Crypto API
- @std/csv – Reading and writing of comma-separated values (CSV) files
- @std/data-structures – Common data structures like red-black trees and binary heaps
- @std/datetime – UNSTABLE: Utilities for dealing with Date objects
- @std/dotenv – UNSTABLE: Parsing and loading environment variables from a
.envfile - @std/encoding – Utilities for encoding and decoding common formats like hex, base64, and varint
- @std/expect – Jest compatible
expectassertion functions - @std/fmt – Utilities for formatting values, such as adding colors to text, formatting durations, printf utils, formatting byte numbers.
- @std/front-matter – Extract front matter from strings
- @std/fs – Helpers for working with the file system
- @std/html – Functions for HTML, such as escaping or unescaping HTML entities
- @std/http – Utilities for building HTTP servers
- @std/ini – UNSTABLE: Parsing and serializing of INI files
- @std/internal – INTERNAL: The internal package for @std. Do not use this directly.
- @std/io – UNSTABLE: The utilities for advanced I/O operations using Reader and Writer interfaces.
- @std/json – (Streaming) parsing and serializing of JSON files
- @std/jsonc – Parsing and serializing of JSONC files
- @std/log – UNSTABLE: A customizable logger framework
- @std/media-types – Utility functions for media types (MIME types)
- @std/msgpack – Encoding and decoding for the msgpack format
- @std/net – Utilities for working with the network
- @std/path – Utilities for working with file system paths
- @std/random – UNSTABLE: Various utilities using random number generators. The package also provides seeded pseudo-random number generator.
- @std/regexp – Utilities for working with RegExp
- @std/semver – Parsing and comparing of semantic versions (SemVer)
- @std/streams – Utilities for working with the Web Streams API
- @std/tar – UNSTABLE: Streaming utilities for working with tar archives.
- @std/testing – Tools for testing Deno code like snapshot testing, bdd testing, and time mocking
- @std/text – Utilities for working with text
- @std/toml – Parsing and serializing of TOML files
- @std/ulid – Generation of Universally Unique Lexicographically Sortable Identifiers (ULIDs)
- @std/uuid – Generators and validators for UUIDs
- @std/webgpu – UNSTABLE: Utilities for working with the Web GPU API
- @std/yaml – Parsing and serializing of YAML files
版本控制和稳定性 Jump to heading
标准库的每个包都独立版本管理。包遵循语义版本控制规则。你可以使用版本锁定或版本范围来防止主版本发布影响你的代码。
导入标准库模块 Jump to heading
要安装 Deno 标准库的包,你可以使用 deno add 子命令将包添加到你的 deno.json 导入映射中。
deno add jsr:@std/fs jsr:@std/path
deno.json 文件的 imports 字段将更新,包含这些导入:
{
"imports": {
"@std/fs": "jsr:@std/fs@^1.0.2",
"@std/path": "jsr:@std/path@^1.0.3"
}
}
然后你可以在源代码中导入这些包:
import { copy } from "@std/fs";
import { join } from "@std/path";
await copy("foo.txt", join("dist", "foo.txt"));
或者,你也可以直接使用 jsr: 规范符导入模块:
import { copy } from "jsr:@std/fs@^1.0.2";
import { join } from "jsr:@std/path@^1.0.3";
await copy("foo.txt", join("dist", "foo.txt"));
Node.js 兼容性 Jump to heading
Deno 标准库设计时兼顾兼容 Node.js、Cloudflare Workers 及其他 JavaScript 环境。标准库以 TypeScript 编写并编译为 JavaScript,因此可以在任何 JavaScript 环境中使用。
npx jsr add @std/fs @std/path
执行此命令会将这些包添加到你的 package.json 中:
{
"dependencies": {
"@std/fs": "npm:@jsr/std__fs@^1.0.2",
"@std/path": "npm:@jsr/std__path@^1.0.3"
}
}
然后你可以像使用其他 Node.js 包一样在源代码中导入它们。TypeScript 会自动找到这些包的类型定义。
import { copy } from "@std/fs";
import { join } from "@std/path";
await copy("foo.txt", join("dist", "foo.txt"));