read-file-safe
Read files without try catch
Last updated 2 years ago by bconnorwhite .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install read-file-safe 
SYNC missed versions from official npm registry.

read-file-safe

NPM TypeScript Coverage Status

Read files without try catch.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


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); // Promise<string | undefined>

const file = readFileSync(path); // string | undefined

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 }); // Promise<Buffer | undefined>

const file = readFileSync(path, { buffer: true }); // Buffer | undefined

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" }); // Promise<string | undefined>

const file = readFile(path, { compression: "brotli" }); // Promise<string | undefined>

Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

License license

MIT - MIT License


Related Packages

Current Tags

  • 2.1.0                                ...           latest (2 years ago)

13 Versions

  • 2.1.0                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
  • 1.0.10                                ...           4 years ago
  • 1.0.9                                ...           4 years ago
  • 1.0.8                                ...           4 years ago
  • 1.0.7                                ...           4 years ago
  • 1.0.6                                ...           4 years ago
  • 1.0.5                                ...           4 years ago
  • 1.0.4                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 13
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (4)

Copyright 2013 - present © cnpmjs.org | Home |