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 🙏

© 2026 – Pkg Stats / Ryan Hefner

magic-file

v1.0.0

Published

Dectect the mimetype of thousands of files using the power of libmagic. Works in Node.js, browsers, and bundlers.

Readme

magic-file

Get the MIME type of virtually any file format using libmagic, the same engine behind the Unix file command, compiled to WebAssembly.

Unlike most browser-friendly file type detectors that recognize only a limited set of signatures, magic-file leverages the full power of libmagic to identify thousands of file formats that have been introduced over the years.

It works everywhere JavaScript runs:

  • ✅ Browser
  • ✅ React
  • ✅ Next.js
  • ✅ Vite
  • ✅ Node.js
  • ✅ Bun
  • ✅ Deno
  • ✅ Most modern bundlers

There are already excellent packages such as file-type, but they intentionally support a relatively small collection of common file signatures. magic-file is designed for applications that need much broader coverage while still remaining easy to use.

Internally, this project uses a WebAssembly build of libmagic, inspired by Colin Kennedy's work: - https://github.com/moshen/wasmagic - https://github.com/file/file - https://man7.org/linux/man-pages/man3/libmagic.3.html

The WASM build has been heavily optimized so it can run reliably in browsers as well as server-side runtimes.

Live Demo

Try magic-file in your browser without installing anything:

🌐 https://filetypecheck.com/

The demo runs entirely in your browser—your files are never uploaded or sent to a server. Drag and drop a file (or enter a URL) to instantly detect its real MIME type and validate whether its extension matches its actual content. :contentReference[oaicite:0]{index=0}

Why is the package around 600KB?

magic-file embeds the compiled magic database containing signatures for thousands of known file formats.

Instead of shipping several megabytes of data, the database is compressed and baked directly into the WASM binary.

The final download is approximately 600KB, and when served through gzip or Brotli (Next.js, React, Vite, CDN, etc.) it becomes even smaller over the network.

The result is a single package with:

  • No native compilation
  • Minimal runtime dependencies
  • Cross-platform compatibility
  • File detection powered by libmagic

Why magic-file?

There are already excellent packages such as file-type, but they intentionally support a relatively small collection of common file signatures. magic-file is designed for applications that require much broader coverage by leveraging the same detection engine used by the Unix file command. :contentReference[oaicite:1]{index=1}


Installation

npm install magic-file

Quick Example

import { Magic } from "magic-file";

const magic = await Magic.create();

const mime = await magic.detect(buffer);
const mime2 = await magic.detect(arrayBuffer);
const mime3 = await magic.detect(file);
const mime4 = await magic.detect(blob);

magic.dispose();

Browser (React / Next.js / Vite)

import { Magic } from "magic-file";

const handleFileChange = async (e) => {
  const files = Array.from(e.target.files).slice(0, 6);

  if (!files.length) return;

  const start = performance.now();

  setLoading(true);
  setResults([]);

  let magic;

  try {
    magic = await Magic.create();

    const results = [];

    for (const file of files) {
      const mime = await magic.detect(file);
      results.push({
        name: file.name,
        mime
      });
    }

    console.log(`${performance.now() - start}ms`);

    setResults(results);

  } finally {
    magic?.dispose();
    setLoading(false);
  }
};

<input type="file" multiple onChange={handleFileChange} />

Node.js

const { Magic } = require("magic-file");
const { readFile } = require("node:fs/promises");

async function detect(path) {

    const magic = await Magic.create();

    try {
        const buffer = await readFile(path);

        const mime = await magic.detect(buffer);

        console.log(mime);

    } finally {
        magic.dispose();
    }
}

detect("./package.json");

Bun

import { Magic } from "magic-file";

const magic = await Magic.create();

try {

    const file = Bun.file("./package.json");

    const mime = await magic.detect(
        new Uint8Array(await file.arrayBuffer())
    );

    console.log(mime);

} finally {
    magic.dispose();
}

Features

  • Full libmagic powered detection
  • Supports thousands of file formats
  • Browser, Node.js, Bun and Deno support
  • React, Next.js and Vite compatible
  • Zero production dependencies
  • No native compilation
  • Single embedded magic database
  • Dual ESM / CommonJS
  • Supports Buffer, Uint8Array, ArrayBuffer, Blob and File
  • Small API: create(), detect(), dispose()

Acknowledgements

magic-file is powered by libmagic, the same file identification library used by the Unix file command.

This project builds upon the WebAssembly work originally started by Colin Kennedy:

  • https://github.com/moshen/wasmagic

Special thanks to the libmagic project and its maintainers, including Ian F. Darwin and Christos Zoulas, for creating and maintaining one of the most comprehensive file identification libraries available.

License

BSD-2-Clause