Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Standard Library and Web APIs

Path, FS and Data Formats

Handle paths, walk directory trees and glob files with @std/path and @std/fs, and read or write JSON, CSV, YAML and TOML in Deno with the standard library and built-in JSON.

Programs spend a lot of time moving files around and converting data between formats. Deno gives you the basics in the Deno namespace (Reading and Writing Files), and the standard library adds the comfortable layer on top: path helpers that work on every operating system, fs utilities for trees of files, and parsers for JSON, CSV, YAML and TOML.

The @std imports are read-only here, since Deno is not available on this site. The runnable examples use Node.js's path and plain JavaScript for the same ideas.

Paths with @std/path

Never join paths with + or "/". Windows uses backslashes, and double separators or .. segments break things. @std/path does it correctly on every system:

TypeScript
import { basename, dirname, extname, join, normalize, relative, resolve, parse } from "@std/path";

join("data", "users", "ada.json");     // "data/users/ada.json" (data\users\ada.json on Windows)
resolve("data", "users");               // absolute path from the current directory
basename("/a/b/report.final.pdf");      // "report.final.pdf"
basename("/a/b/report.final.pdf", ".pdf"); // "report.final"
extname("/a/b/report.final.pdf");       // ".pdf"
dirname("/a/b/report.pdf");             // "/a/b"
relative("/a/b/c", "/a/d");             // "../../d"
normalize("a//b/../c");                 // "a/c"

parse splits a path into root, dir, base, name and ext, and format builds one back. The package also offers isAbsolute, globToRegExp, fromFileUrl and toFileUrl. There are explicit posix and win32 versions (import * as path from "@std/path/posix") for when you must produce one style regardless of the host system, for example when building a URL path.

Node.js's path module has the same functions, so you can try the logic:

Node.js Path manipulation

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.

Directory work with @std/fs

@std/fs handles trees of files:

TypeScript
import { copy, ensureDir, ensureFile, exists, expandGlob, move, walk } from "@std/fs";

await ensureDir("out/reports");              // like mkdir -p
await ensureFile("out/log.txt");             // create file and parents if needed
await copy("src", "backup", { overwrite: true }); // recursive copy
await move("old.txt", "new.txt");

console.log(await exists("config.json"));    // handy; keep race conditions in mind

for await (const entry of walk("src", { exts: [".ts"], skip: [/node_modules/] })) {
  console.log(entry.path);
}

for await (const file of expandGlob("**/*.test.ts")) {
  console.log(file.path);
}

walk visits every file below a folder, lazily, with filters (exts, match, skip, maxDepth, includeDirs). expandGlob takes a glob pattern such as src/**/*.ts.

The idea of walking a tree is a recursive function. This stand-in walks an in-memory tree, so the logic is clear:

Node.js Walking a tree recursively

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.

JSON

JSON support is built into the language; no import is needed:

TypeScript
const text = await Deno.readTextFile("config.json");
const config = JSON.parse(text);

await Deno.writeTextFile("config.json", JSON.stringify(config, null, 2) + "\n");

A few details make JSON handling robust:

  • JSON.stringify(value, null, 2) pretty-prints; pass a replacer function or an array of keys to filter.
  • JSON.parse(text, reviver) can convert strings into dates or other types while parsing.
  • JSON.parse throws SyntaxError on bad input. Catch it and report the file name.
  • JSON cannot hold undefined, functions, Map, Set or BigInt; convert first.
  • For files with comments, use @std/jsonc's parse.
Node.js Pretty printing and revivers

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.

CSV

TypeScript
import { parse, stringify } from "@std/csv";

const rows = parse("name,age\nAda,36\nLinus,55\n", { skipFirstRow: true });
// [{ name: "Ada", age: "36" }, { name: "Linus", age: "55" }]

console.log(stringify(rows, { columns: ["name", "age"] }));

Values always come back as strings, so convert numbers yourself. Quoted fields can contain commas and line breaks, which is why splitting on commas is not a real CSV parser. A small one shows the problem:

Node.js A CSV line parser that respects quotes

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.

YAML and TOML

Configuration files often use YAML or TOML. The standard library parses and writes both:

TypeScript
import { parse as parseYaml, stringify as stringifyYaml } from "@std/yaml";
import { parse as parseToml } from "@std/toml";

const config = parseYaml(`
server:
  port: 8080
  tags: [web, api]
`);
console.log(config.server.port); // 8080

const toml = parseToml(`
title = "My app"

[database]
host = "localhost"
port = 5432
`);
console.log(toml.database.host); // localhost

Use stringify to write a config back. YAML has many traps (the famous "Norway problem" where no is read as false in old YAML versions), so quote strings that look like other types.

A complete example

Read a CSV of products, filter, and write JSON next to it:

TypeScript
import { parse } from "@std/csv";
import { join } from "@std/path";
import { ensureDir } from "@std/fs";

const rows = parse(await Deno.readTextFile("products.csv"), { skipFirstRow: true });
const cheap = rows
  .map((r) => ({ name: r.name, price: Number(r.price) }))
  .filter((p) => p.price < 20);

await ensureDir("out");
await Deno.writeTextFile(join("out", "cheap.json"), JSON.stringify(cheap, null, 2));
console.log(`wrote ${cheap.length} products`);
Terminal
deno run --allow-read=products.csv --allow-write=out main.ts

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use join() from @std/path instead of string concatenation?

    Separators differ between systems, and join also normalises odd segments.
  2. 2. What type are the values returned by the CSV parser?

    CSV has no types, so every field is a string and you convert it yourself.
  3. 3. Which function visits every file below a directory lazily?

    walk recurses through the tree and supports filters; Deno.readDir lists a single directory.
  4. 4. How do you drop a field while converting an object to JSON?

    A replacer can return undefined for keys you want to omit.

Summary

  • @std/path gives cross-platform join, resolve, basename, extname, relative and parse.
  • @std/fs adds ensureDir, copy, move, walk and expandGlob.
  • JSON is built in; use replacers, revivers and pretty-printing, and catch SyntaxError.
  • @std/csv, @std/yaml and @std/toml parse other formats; CSV values are strings.
  • Give the program only the read and write permissions it needs.

Next: Web Crypto, Randomness and Timers