Skip to content

Path.writeText() method

pathlib-ts > Path > writeText

Asynchronously write the given text to the file using the provided encoding.

An existing file of the same name is overwritten.

Signature:

writeText(data: string, encoding?: BufferEncoding): Promise<void>;

Parameter

Type

Description

data

string

Text to persist.

encoding

BufferEncoding

(Optional) Encoding to use (defaults to "utf8").

Returns:

Promise<void>

Promise that settles once the write completes.

Persisting cache data asynchronously

import { Path } from "pathlib-ts";
const cache = new Path("./.cache/state.json");
await cache.writeText(JSON.stringify({ refreshed: Date.now() }));