Skip to main content

File

Promise-based reads, writes, existence checks and deletes over node:fs/promises, plus a file-extension parser. Node-only, async-only and UTF-8 only — no sync variants and no encoding argument, since those are exactly the cases where you should reach for node:fs directly. fileExists answers a question about the past: the file can disappear before your next line runs, so where it matters, attempt the operation and handle the failure instead.

Example

import {
getFileExtension,
readFileAsString,
writeFileAsString,
} from '@rtorcato/js-common/file'

// Read, transform, write back — UTF-8 and promise-based, no fs boilerplate.
const raw = await readFileAsString('config.json')
await writeFileAsString('config.json', raw.replaceAll('http://', 'https://'))

getFileExtension('/tmp/report.tar.gz') // '.gz' — leading dot, like path.extname

Import

import { deleteFile, fileExists, getFileExtension } from '@rtorcato/js-common/file'

Exports

NameSummary
deleteFileDeletes a file.
fileExistsChecks if a file exists.
getFileExtensionGets the file extension from a file path.
readFileAsStringReads a file as a string (UTF-8).
writeFileAsStringWrites a string to a file (UTF-8).

See also

  • mime-types — look up MIME types by file extension
  • node — Node version checks and optional requires
  • os — platform, arch, home and temp directories
  • process — cwd, pid, uptime, exit, CI detection