Skip to content

readFileSync ​

readFileSync returns a file's UTF-8 contents as Ok(string), or an FsError as Err.

SignaturereadFileSync(path: string): Result<string, FsError>
tyrus
import { readFileSync, type FsError } from "tyrus:fs";

match (readFileSync("README.md")) {
    Ok(readme) => { print(readme); }
    Err(error) => { printError(error.code); // ERR_FS_READ_FAILED
    }
}

Error Handling ​

If the file cannot be read, readFileSync returns Err(FsError) with a stable code field (e.g. ERR_FS_READ_FAILED, ERR_FS_NOT_FOUND) and a human-readable message. Inspect the result with match or propagate it with ?:

error

tyrus
import { readFileSync, type FsError } from "tyrus:fs";

match (readFileSync("README.md")) {
    Ok(readme) => { print(readme); }
    Err(error) => { printError(error.code); // ERR_FS_READ_FAILED
    }
}

With Explicit Types ​

tyrus
import { readFileSync, type FsError } from "tyrus:fs";

const main = (): void => {
    const path: string = "config.json";
    match (readFileSync(path)) {
        Ok(contents) => print(contents);
        Err(error) => printError(error.message);
    }
};