Read files without try catch
npm install read-file-safeRead files without try catch.
_If I should maintain this repo, please ⭐️_
_DM me on Twitter if you have questions or suggestions._
---
This package handles filesystem errors for you. If a file does not exist or cannot be accessed, undefined is returned instead of throwing an error.
``sh`
yarn add read-file-safe
`sh`
npm install read-file-safe
`sh`
pnpm add read-file-safe
By default, readFile and readFileSync return a string:
`ts
import { readFile, readFileSync } from "read-file-safe";
const path = "./path/to/file.ext";
const file = readFile(path); // Promise
const file = readFileSync(path); // string | undefined
`
Setting the buffer option to true will return a Buffer instead of a string:
`ts
import { readFile, readFileSync } from "read-file-safe";
const file = readFile(path, { buffer: true }); // Promise
const file = readFileSync(path, { buffer: true }); // Buffer | undefined
`
Setting the compression option will decompress the file before returning it. Both gzip and brotli are supported:
`ts
import { readFile, readFileSync } from "read-file-safe";
const file = readFile(path, { compression: "gzip" }); // Promise
const file = readFile(path, { compression: "brotli" }); // Promise
``
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.
MIT - _MIT License_
- fs-safe: A simple fs wrapper that doesn't throw
- write-file-safe: Write files, and parent directories if necessary
- remove-file-safe: Remove files without try catch
- read-dir-safe: Read directories recursively or non-recursively
- write-dir-safe: Create directories and their parents recursively
- remove-dir-safe: Remove directories recursively or non-recursively