# read-file-safe

> Read files without try catch

Latest version **3.0.0** (published 2026-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install read-file-safe
pnpm add read-file-safe
yarn add read-file-safe
bun add read-file-safe
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2026-03-11 |
| First published | 2020-08-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.0.0 |
| Dependencies | 0 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Connor White |
| Maintainers | bconnorwhite |
| Keywords | read, file, safe, try, catch, ENOENT, fs, fs-safe |

## Links

- npm: https://www.npmjs.com/package/read-file-safe
- Repository: https://github.com/bconnorwhite/read-file-safe
- Homepage: https://github.bconnorwhite.com/read-file-safe#readme
- Issues: https://github.com/bconnorwhite/read-file-safe/issues
- npm.io page: https://npm.io/package/read-file-safe

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 3.0.0 (latest) — 2026-03-11
- 2.1.1 — 2023-09-19
- 2.1.0 — 2023-01-12
- 2.0.0 — 2023-01-11
- 1.0.10 — 2020-10-05
- 1.0.9 — 2020-10-04
- 1.0.8 — 2020-10-04
- 1.0.7 — 2020-10-03
- 1.0.6 — 2020-10-03
- 1.0.5 — 2020-09-02
- 1.0.4 — 2020-09-02
- 1.0.3 — 2020-08-28
- 1.0.2 — 2020-08-28
- 1.0.1 — 2020-08-16
- 1.0.0 — 2020-08-16

## README

<!--BEGIN HEADER-->
<div id="top" align="center">
  <h1>read-file-safe</h1>
  <a href="https://npmjs.com/package/read-file-safe">
    <img alt="NPM" src="https://img.shields.io/npm/v/read-file-safe.svg">
  </a>
  <a href="https://github.com/bconnorwhite/read-file-safe">
    <img alt="TypeScript" src="https://img.shields.io/github/languages/top/bconnorwhite/read-file-safe.svg">
  </a>
  <a href="https://coveralls.io/github/bconnorwhite/read-file-safe?branch=master">
    <img alt="Coverage Status" src="https://img.shields.io/coveralls/github/bconnorwhite/read-file-safe.svg?branch=master">
  </a>
</div>

<br />

<blockquote align="center">Read files without try catch.</blockquote>


---
<!--END HEADER-->

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

<details open>
  <summary>
    <a href="https://www.npmjs.com/package/read-file-safe">
      <img src="https://img.shields.io/badge/npm-CB3837?logo=npm&logoColor=white" alt="NPM" />
    </a>
  </summary>

```sh
npm install read-file-safe
```

</details>

<details>
  <summary>
    <a href="https://yarnpkg.com/package/read-file-safe">
      <img src="https://img.shields.io/badge/yarn-2C8EBB?logo=yarn&logoColor=white" alt="Yarn" />
    </a>
  </summary>

```sh
yarn add read-file-safe
```

</details>

<details>
  <summary>
    <img src="https://img.shields.io/badge/pnpm-F69220?logo=pnpm&logoColor=white" alt="PNPM" />
  </summary>

```sh
pnpm add read-file-safe
```

</details>

<details>
  <summary>
    <img src="https://img.shields.io/badge/bun-EE81C3?logo=bun&logoColor=white" alt="Bun" />
  </summary>

```sh
bun add read-file-safe
```

</details>


## Usage

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<string | undefined>

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

### Return Type

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<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:

```ts
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>
```

<!--BEGIN FOOTER-->
<h2 id="license">License <a href="https://opensource.org/licenses/MIT"><img align="right" alt="license" src="https://img.shields.io/npm/l/read-file-safe.svg"></a></h2>

[MIT](https://opensource.org/licenses/MIT) - _MIT License_
<!--END FOOTER-->

<br />

## Related Packages

- [fs-safe](https://www.npmjs.com/package/fs-safe): A simple fs wrapper that doesn't throw
- [write-file-safe](https://www.npmjs.com/package/write-file-safe): Write files, and parent directories if necessary
- [remove-file-safe](https://www.npmjs.com/package/remove-file-safe): Remove files without try catch
- [read-dir-safe](https://www.npmjs.com/package/read-dir-safe): Read directories recursively or non-recursively
- [write-dir-safe](https://www.npmjs.com/package/write-dir-safe): Create directories and their parents recursively
- [remove-dir-safe](https://www.npmjs.com/package/remove-dir-safe): Remove directories recursively or non-recursively

---
_Source: https://npm.io/package/read-file-safe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
