Deno’s standard library (std/) provides a rich set of out-of-the-box modules, adhering to modern JavaScript/TypeScript best practices. Below is a categorized overview of core modules and their functionalities:
Core Foundation Modules
File System Operations (std/fs)
| Module | Functionality | Example |
|---|---|---|
fs/read_file.ts | Read file content | const text = await Deno.readTextFile("file.txt") |
fs/write_file.ts | Write to file | await Deno.writeTextFile("file.txt", "Hello") |
fs/copy.ts | Copy files/directories | await copy("src/", "dest/", { overwrite: true }) |
fs/ensure_file.ts | Ensure file exists | await ensureFile("data.json") |
fs/exists.ts | Check if path exists | const exists = await exists("file.txt") |
Features:
- All operations require explicit
--allow-read/--allow-writepermissions. - Supports Promise-based asynchronous APIs.
- Provides atomic operations (e.g.,
copywithoverwriteoption).
Network Requests (std/http)
| Module | Functionality | Example |
|---|---|---|
http/server.ts | Create HTTP server | const server = serve({ port: 8000 }) |
http/client.ts | Send HTTP requests | const resp = await fetch("https://api.example.com") |
http/cookie.ts | Cookie parsing utility | parseCookies(req.headers) |
Typical Usage:
// Create a basic HTTP server
import { serve } from "std/http/server.ts";
const server = serve({ port: 8000 });
for await (const req of server) {
req.respond({ body: "Hello Deno!" });
}Path Handling (std/path)
| Module | Functionality | Example |
|---|---|---|
path/mod.ts | Cross-platform path operations | join("dir", "file.txt") → "dir/file.txt" |
path/win32.ts | Windows-specific path handling | normalize("C:\\dir\\..\\file") |
Key Functions:
join(...paths): Smart path concatenation.basename(path): Extract filename.extname(path): Extract file extension.
Advanced Functionality Modules
Data Parsing (std/encoding)
| Module | Functionality | Example |
|---|---|---|
encoding/json.ts | JSON encoding/decoding | const obj = JSON.parse('{"a":1}') |
encoding/base64.ts | Base64 conversion | encode("text") / decode("dGV4dA==") |
encoding/csv.ts | CSV parsing | parseCsv("a,b\nc,d") |
Notes:
- JSON operations use the global
JSONobject (consistent with browsers). - Base64 supports URL-safe mode.
Date and Time (std/datetime)
| Module | Functionality | Example |
|---|---|---|
datetime/mod.ts | Date formatting | format(new Date(), "yyyy-MM-dd") |
datetime/iso8601.ts | ISO8601 parsing | parseIso8601("2023-01-01T00:00:00Z") |
Unique Features:
- Supports multiple date parsing formats.
- Time zone conversion utilities.
Logging System (std/log)
| Module | Functionality | Example |
|---|---|---|
log/mod.ts | Multi-level logging | logger.info("User login") |
log/levels.ts | Log level control | logger.setLevel("DEBUG") |
Log Levels:
DEBUG|INFO|WARNING|ERROR|CRITICAL
Utility Modules
Testing Framework (std/testing)
| Module | Functionality | Example |
|---|---|---|
testing/asserts.ts | Assertion library | assertEquals(1, 1) |
testing/bench.ts | Benchmark testing | bench(() => heavyTask()) |
Assertion Methods:
assertEquals(actual, expected)assertThrows(fn, ErrorClass?)
Random Number Generation (std/random)
| Module | Functionality | Example |
|---|---|---|
random/mod.ts | Secure random numbers | randomBytes(32) |
random/uuid.ts | UUID generation | uuidv4() |
Security Note:
randomBytesuses OS-level cryptographically secure APIs.- Suitable for generating keys, tokens, and other sensitive data.
Signal Handling (std/signal)
| Module | Functionality | Example |
|---|---|---|
signal/mod.ts | Process signal listening | onSignal("SIGTERM", () => cleanup()) |
Supported Signals:
SIGINT(Ctrl+C)SIGTERM(Graceful termination)
Web Development Related
Web Utilities (std/web)
| Module | Functionality | Example |
|---|---|---|
web/cookie.ts | Cookie operations | serializeCookie("name", "value") |
web/storage.ts | Web Storage simulation | sessionStorage.set("key", "value") |
Use Cases:
- Frontend compatibility in Deno Deploy environments.
- Server-side simulation of browser APIs.
Best Practices for Module Usage
Centralized Dependency Management (deps.ts)
// deps.ts
export { serve } from "https://deno.land/std@0.140.0/http/server.ts"
export { assertEquals } from "https://deno.land/std@0.140.0/testing/asserts.ts"
// main.ts
import { serve, assertEquals } from "./deps.ts"Advantages:
- Unified version control.
- Simplified import paths.
Principle of Least Privilege
# Grant only necessary permissions
deno run --allow-net=api.example.com --allow-read=./data main.tsSecurity Recommendations:
- Avoid using
--allow-all. - Fine-tune permission scopes (e.g., specify allowed domains).
Leveraging the Caching Mechanism
# Manually pre-cache dependencies
deno cache --import-map=import_map.json main.ts
# View cache location
deno infoCache Directory:
- Defaults to
~/.deno/dir(customizable viaDENO_DIRenvironment variable).
Standard Library Updates and Version Control
Version Specification:
// Use specific version
import { serve } from "https://deno.land/std@0.140.0/http/server.ts"
// Or manage uniformly via import map
{
"imports": {
"std/": "https://deno.land/std@0.140.0/"
}
}Upgrade Strategy:
- Regularly check official release notes.
- Use
deno upgradeto update the runtime.
By effectively combining these standard library modules, developers can rapidly build high-performance, secure Deno applications while maintaining code simplicity and maintainability.



