Nodejs, to get file type of a file

Viewed 6949
1 Answers

OPTION 1: If you want the mimetype:

Install

npm i -S file-type read-chunk

Use

const readChunk = require('read-chunk');
const fileType = require('file-type');

const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
console.log(fileType(buffer));

OPTION 2: If you just need the would-be file extension:

Install

npm i -S image-size

Use

const sizeOf = require('image-size');
console.log(sizeOf(filePath).type);
Related