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.
Maintainers
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-fileQuick 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,BlobandFile - 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
