npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

isbinaryfile

v6.0.0

Published

Detects if a file is binary in Node.js. Similar to Perl's -B.

Downloads

37,324,880

Readme

isBinaryFile

Detects if a file is binary in Node.js. Similar to Perl's -B switch, in that:

  • it reads the first few thousand bytes of a file
  • checks for a null byte; if it's found, it's binary
  • flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary

Much of the logic is pretty much ported from ag.

Note: if the file doesn't exist or is a directory, an error is thrown.

Installation

npm install isbinaryfile

Usage

Returns Promise<boolean> (or just boolean for *Sync). true if the file is binary, false otherwise.

isBinaryFile(filepath[, options])

  • filepath - a string indicating the path to the file.
  • options - an optional object with the following properties:

isBinaryFile(bytes[, options])

  • bytes - a Buffer of the file's contents.
  • options - an optional object with the following properties:
    • size - the size of the buffer (defaults to bytes.length)
    • encoding - an encoding hint (see Encoding Hints below)

isBinaryFileSync(filepath[, options])

Synchronous version of isBinaryFile.

isBinaryFileSync(bytes[, options])

Synchronous version of isBinaryFile for buffers.

Examples

Here's an arbitrary usage:

import { isBinaryFile, isBinaryFileSync } from 'isbinaryfile';
import fs from 'fs';

const filename = 'fixtures/pdf.pdf';

// Async with file path
const result = await isBinaryFile(filename);
if (result) {
  console.log('It is binary!');
} else {
  console.log('No it is not.');
}

// Sync with buffer
const bytes = fs.readFileSync(filename);
console.log(isBinaryFileSync(bytes)); // true or false

// With explicit size option
const partialBuffer = Buffer.alloc(100);
fs.readSync(fs.openSync(filename, 'r'), partialBuffer, 0, 100, 0);
console.log(isBinaryFileSync(partialBuffer, { size: 100 }));

Encoding Hints

For files that use non-UTF-8 encodings, you can provide encoding hints to improve detection accuracy:

import { isBinaryFile, isBinaryFileSync } from 'isbinaryfile';

// UTF-16 files without BOM are auto-detected in most cases
const result1 = await isBinaryFile('utf16-file.txt');

// Or provide explicit encoding hint
const result2 = await isBinaryFile('utf16-file.txt', { encoding: 'utf-16' });

// ISO-8859-1 / Latin-1 encoded files
const result3 = isBinaryFileSync('german-text.txt', { encoding: 'latin1' });

// CJK encoded files (Big5, GB2312, EUC-KR, etc.)
const result4 = isBinaryFileSync('chinese-big5.txt', { encoding: 'big5' });
const result5 = isBinaryFileSync('korean-text.txt', { encoding: 'euc-kr' });

// Generic CJK hint when exact encoding is unknown
const result6 = isBinaryFileSync('asian-text.txt', { encoding: 'cjk' });

Supported Encoding Hints

| Hint | Description | | ------------ | ------------------------------------------ | | utf-16 | UTF-16 (auto-detect endianness) | | utf-16le | UTF-16 Little Endian | | utf-16be | UTF-16 Big Endian | | latin1 | ISO-8859-1 / Latin-1 | | iso-8859-1 | Alias for latin1 | | cjk | Generic CJK (use when encoding is unknown) | | big5 | Traditional Chinese | | gb2312 | Simplified Chinese | | gbk | Extended GB2312 | | euc-kr | Korean | | shift-jis | Japanese |

Note: UTF-16 without BOM is automatically detected in most cases without needing a hint.

Testing

Run npm test.