Read 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.
Installation
yarn add read-file-safe
npm install read-file-safe
pnpm add read-file-safe
Usage
By default, readFile
and readFileSync
return a string:
import { readFile, readFileSync } from "read-file-safe";
const path = "./path/to/file.ext";
const file = readFile(path);
const file = readFileSync(path);
Return Type
Setting the buffer
option to true
will return a Buffer
instead of a string:
import { readFile, readFileSync } from "read-file-safe";
const file = readFile(path, { buffer: true });
const file = readFileSync(path, { buffer: true });
Decompression
Setting the compression
option will decompress the file before returning it. Both gzip
and brotli
are supported:
import { readFile, readFileSync } from "read-file-safe";
const file = readFile(path, { compression: "gzip" });
const file = readFile(path, { compression: "brotli" });
Dev Dependencies
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.
License
MIT - MIT License
Related Packages