I want the output to be like image/png. type then followed by the extension. The filename doesn't include any information.
I tried basics such as mediainfo and ffprobe but one showed missing libraries error and the other doesn't work with images. I also
You can use the fs module (built-in) and file-type package (install with npm):
npm install file-type
with code like:
const fs = require('fs');
const fileType = require('file-type');
const getFileType = async(filePath) {
const fileBuffer = fs.readFileSync(filePath);
const type = await fileType.fromBuffer(fileBuffer);
if (type) {
console.log(`${type.mime} (${type.ext})`);
return `${type.mime}`
} else {
console.log('Unable to determine the file type.');
return 'unknown';
}
}
const filename = 'path/to/your/file';
getFileType(filename)
.then((result) => console.log('MIME type:', result))
.catch((err) => console.error('Error:', err));