In certain cases, you may come across the need to determine the extension of a file, such as an image file. To accomplish this task effectively in Node.js, you can utilize the path
built-in module and its extname()
method. Below is an example of how to use this method:
const path = require('path');
path.extname('picture.png'); //.png
path.extname('picture.of.a.dog.png'); //.png
path.extname('picture.of.a.dog.jpg'); //.jpg
path.extname('picture.of.a.dog.jpeg'); //.jpeg
By employing the path
module and its extname()
method, you can easily extract the extension from the given file name.