Skip to Content
APItruncate

truncate

Truncate or extend a file to the given length. If the file is longer, extra bytes are discarded; if shorter, it is zero-padded (or platform behavior).

Basic usage

import { truncate } from '@vooya/fs' await truncate('./file.txt', 100) // trim or extend to 100 bytes await truncate('./file.txt') // truncate to 0

Methods

truncate(path, len?)

Async. Returns Promise<void>.

ArgumentTypeDescription
pathstringFile path.
lennumberOptional. Target length in bytes; default 0.

truncateSync(path, len?)

Sync. Same arguments; throws on error.

Performance

Single syscall; on par with Node.js. See Benchmarks.

Notes

  • Extension: When extending, behavior may be platform-dependent (zero-fill or sparse); same as Node.js.
  • Negative lengths: Current supported Node versions clamp negative values to 0; Vooya FS matches that observed behavior.
  • Known gap: Missing-path errors currently have Node-like messages, but do not expose Node-style code, path, and syscall fields yet.
  • Unsupported Node inputs/APIs: Buffer paths, URL paths, and file-descriptor ftruncate APIs are not part of the current Vooya FS truncate surface.

Competitor measurements

See the full truncate comparison table for Node 22/24, peer libraries, sync/Promise modes, small and batch workloads, execution routes and measurement limits.

Last updated on