Skip to content

File System

The std/fs module provides asynchronous file read/write operations and directory management.

import { fs } from "std/fs"

Reads a file asynchronously and returns its contents as a string.

const content = await fs.readFile("./config.json")
println(content)

fs.writeFile(path: string, content: string): Task<void>

Section titled “fs.writeFile(path: string, content: string): Task<void>”

Writes content to a file asynchronously. Creates the file if it doesn’t exist, or overwrites it if it does.

await fs.writeFile("./log.txt", "Log entry")

fs.appendFile(path: string, content: string): Task<void>

Section titled “fs.appendFile(path: string, content: string): Task<void>”

Appends content to an existing file.

await fs.appendFile("./log.txt", "\nAnother entry")

Returns true if the given path exists.

if (fs.exists("./config.json")) {
println("Config found")
}

Returns file metadata including size, modification time, and whether it’s a directory.

const info = fs.stat("./data.txt")
println(info["size"]) // File size in bytes
println(info["isDir"]) // false
println(info["modTime"]) // Last modification time

Creates a directory at the given path.

fs.mkdir("./output")

Lists the contents of a directory.

const files = fs.readDir("./src")
files.forEach(function(f: any, i: any, a: any): any {
println(f)
})

Removes a file or directory.

fs.remove("./temp.txt")

Returns the absolute path.

const full = fs.abs("./config.json")
println(full) // e.g. "/home/user/project/config.json"

Joins path segments together.

const p = fs.join("src", "utils", "helpers.chuks")
println(p) // "src/utils/helpers.chuks"
FunctionDescription
fs.readFile(path)Read file contents as string
fs.writeFile(path, content)Write string to file
fs.appendFile(path, content)Append string to file
fs.exists(path)Check if path exists
fs.mkdir(path)Create directory
fs.remove(path)Remove file or directory
fs.readDir(path)List directory contents
fs.stat(path)File metadata (size, modTime, isDir)
fs.abs(path)Absolute path
fs.join(parts...)Join path segments
import { fs } from "std/fs"
import { json } from "std/json"
// Read a JSON config
const raw = await fs.readFile("./config.json")
const config = json.parse(raw)
// Modify and write back
config["version"] = 2
await fs.writeFile("./config.json", json.stringify(config))
println("Config updated")