Skip to content Skip to sidebar Skip to footer

Nodejs, To Get File Type Of A File

The six-year-old question 'Node.js - File System get file type, solution around year 2012' has a better answer but the outdated answer was the correct one by then. Hence the questi

Solution 1:

OPTION 1: If you want the mimetype:

Install

npm i -S file-typeread-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);

Post a Comment for "Nodejs, To Get File Type Of A File"