Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Standard Library and Web APIs

The Deno Standard Library

Explore the Deno standard library on JSR - how @std packages are organised and versioned, key modules like assert, collections, fmt, text, async, cli and dotenv, with usage patterns.

Deno's built-in API is intentionally small. Instead of packing every helper into the runtime, the Deno team maintains the standard library, a collection of reviewed, tested modules published on JSR as @std/.... You import only what you use, versions are explicit, and the code is plain TypeScript you can read.

This lesson maps the library, shows you how to import from it, and walks through the modules you will reach for most often. Later lessons cover the file, path and data modules in more depth (Path, FS and Data Formats).

Deno is not available on this site, so @std imports are read-only. Each section also has a runnable plain-JavaScript implementation of the same idea, so you can see what the function does.

How the library is organised

Each area is its own package with its own version, so a bug fix in one does not force an upgrade of the rest. Most packages are stable at 1.0 or later; a few are still in 0.x or marked unstable, and the docs say so.

Package What it offers
@std/assert assertEquals, assertThrows, ... for tests and checks
@std/testing BDD helpers, mocks, spies, fake time, snapshots
@std/collections groupBy, chunk, partition, distinct, sortBy, ...
@std/path Cross-platform path joining, parsing, resolving
@std/fs walk, ensureDir, copy, expandGlob, exists
@std/streams Line splitting, merging, byte streams
@std/encoding Base64, hex, varint
@std/csv, @std/yaml, @std/toml, @std/jsonc Data format parsers
@std/fmt Colors, byte sizes, durations, printf
@std/text Case conversion, string similarity, slugify-style helpers
@std/async delay, retry, debounce, pooledMap
@std/cli parseArgs, prompts, spinners
@std/dotenv Load .env files
@std/http Helpers for servers: file serving, cookies, status codes
@std/uuid, @std/ulid Identifiers
@std/semver Version parsing and ranges
@std/crypto Extra hash algorithms on top of Web Crypto
@std/datetime Date helpers
@std/html Escape and unescape HTML entities
@std/data-structures Binary heap, red-black tree

Importing

Add the package once, then use the short name:

Terminal
deno add jsr:@std/collections jsr:@std/fmt
TypeScript
import { chunk, groupBy } from "@std/collections";
import { format as formatBytes } from "@std/fmt/bytes";

You can also import inline: import { chunk } from "jsr:@std/collections@1". Many packages offer sub-path exports (@std/fmt/bytes) for importing one module only. Either form works; the full package import is fine because unused code is not run.

Assertions

@std/assert is the foundation of tests, and also handy in scripts as sanity checks:

TypeScript
import { assert, assertEquals, assertThrows, assertAlmostEquals } from "@std/assert";

assertEquals([1, 2, { a: 3 }], [1, 2, { a: 3 }]); // deep equality
assertAlmostEquals(0.1 + 0.2, 0.3, 1e-9);
assertThrows(() => JSON.parse("{"), SyntaxError);
assert(Number.isInteger(5), "must be an integer");

Unlike ===, assertEquals compares objects, arrays, Maps, Sets and dates by value, and prints a readable diff when they differ. Testing and Tooling is a whole chapter on this.

Collections

@std/collections fills gaps in the built-in array and object helpers:

TypeScript
import { chunk, groupBy, partition, distinct, sortBy, mapValues } from "@std/collections";

chunk([1, 2, 3, 4, 5], 2);                 // [[1, 2], [3, 4], [5]]
partition([1, 2, 3, 4], (n) => n % 2 === 0); // [[2, 4], [1, 3]]
distinct([1, 1, 2, 3, 3]);                  // [1, 2, 3]
sortBy(people, (p) => p.age);               // stable sort by a key
groupBy(people, (p) => p.team);             // { red: [...], blue: [...] }

These are small enough to implement yourself, which is the best way to understand them. Here are three:

Node.js Writing chunk, partition and groupBy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Note that modern JavaScript now has Object.groupBy and Map.groupBy, so prefer the built-ins when they cover your case.

Formatting: bytes, durations and colors

@std/fmt turns raw numbers into human-friendly text, and adds terminal colors:

TypeScript
import { format as bytes } from "@std/fmt/bytes";
import { format as duration } from "@std/fmt/duration";
import { green, red, bold } from "@std/fmt/colors";

console.log(bytes(1_536_000));            // "1.54 MB"
console.log(duration(3_723_000));          // "1h 2m 3s"
console.log(bold(green("passed")), red("failed"));

Colors respect NO_COLOR, so output stays clean in logs. A byte formatter is a nice small exercise:

Node.js Human-readable sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Text helpers

@std/text collects string utilities such as case conversion and similarity:

TypeScript
import { toCamelCase, toKebabCase, toSnakeCase, closestString } from "@std/text";

toKebabCase("Hello World Again");        // "hello-world-again"
toCamelCase("user_profile_id");          // "userProfileId"
closestString("colour", ["color", "cold", "cool"]); // "color"

closestString and levenshteinDistance power "did you mean...?" messages in command-line tools.

Choosing and trusting std

A few guidelines keep your use of the library healthy:

  • Check the version. A 0.x package may change between releases; pin it exactly.
  • Prefer built-ins when the web platform or Deno already has the feature (structuredClone, Object.groupBy, crypto.randomUUID()).
  • Read the source when behaviour matters; each module links to it from JSR.
  • Use the docs. Every symbol's JSR page has a runnable example, and deno doc jsr:@std/collections prints them in your terminal.
flowchart TD
  A["Need a helper"] --> B{"Web API or Deno API?"}
  B -->|Yes| C["Use it directly"]
  B -->|No| D{"In @std?"}
  D -->|Yes| E["deno add jsr:@std/..."]
  D -->|No| F["JSR or npm package, or write it"]

Where to look first when you need a helper.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where is the Deno standard library published?

    The @std packages live on JSR and are versioned individually.
  2. 2. What does assertEquals do differently from ===?

    assertEquals compares structures by value and prints a readable diff when they differ.
  3. 3. Which package would you use for the parseArgs function?

    @std/cli holds parseArgs and other command-line helpers.
  4. 4. Why prefer a built-in feature over a std helper when both exist?

    Fewer dependencies and familiar APIs make code simpler and more portable.

Summary

  • The standard library is a set of small, versioned @std/* packages on JSR.
  • Add them with deno add and import by short name or sub-path.
  • @std/assert, collections, fmt, text, async, cli and dotenv cover everyday needs.
  • Pin 0.x packages and prefer platform built-ins when they exist.
  • deno doc shows documentation in the terminal.

Next: fetch and the Web APIs