Learn How to Get File Extensions in Node.js From Any Files

Robin
May 28, 2022

Very often we need to get file extensions in Node.js applications while working with files. In those situations, you can extract the extensions from any files using built-in modules available in NodeJS.

The methods extname() and parse() can be used to get the file extension in Node.js applications from the path built-in module. These methods take the filename, path, or URL as a string and return the extension from the . (period) character to the end of the string.

You can also use an external package like mime-types to get file extensions from a MIME type in your application.

In this article, I will show you all the techniques you can use to obtain an extension of a file using extname() and parse() methods. Not only that, but you will also learn how you can get extensions from the MIME types.

I will give you many examples to show how these methods behave in different situations. This is everything you need to extract file extensions in NodeJS.

Also Read: Perfect Ways to Serve Static Files in Express and Node.js

How to Get File Extensions in Node.js Using extname() Method

First, you have to import the path module to call the extname() method. This method accepts one parameter. You can pass the filename, path, or URL of the file.

When you call this method with the string, it will return the extension of the file. This method actually tracks the (.) character in that string and returns the last part after the (.) character.

Learn How to Get File Extensions in Node.js From Any Types of Files

Getting File Extension From Filenames

Let's call the path.extname() method with the filename. In this section, we will see how it works with the filenames.

Twitch - How to Setup Extensions
          const path = require('path');

path.extname('main.js');
// returns .js

path.extname('profile.png');
// returns .png

path.extname('index.html');
// returns .html
        

At first, you have to import the path module. You can see when I call the extname() method with a filename, it returns the file extension correctly.

It doesn't matter which file type you pass to the method, it will return the extension for any file type.

As you already know that this method identifies the file extension by tracking the (.) character in the string. But what will happen if the string you pass doesn't have the (.) character.

          const path = require('path');

path.extname('profile');
// returns '' (empty string)

path.extname('.profile');
// returns '' (empty string)

path.extname('profile.');
// returns .

path.extname('.profile.png');
// returns .png
        
  • If the string doesn't have any (.) character in the string, the method will return an empty string.
  • If it contains one (.) character at the beginning of the string, the extname() method will return an empty string.
  • If the string contains one (.) character at the end, the method will return the (.) character.
  • If the string has multiple (.) characters, the method will return the last part as the file extension.

But if you don't pass a string in the extname() method as its argument, it will through a TypeError for the wrong type.

Also Read: Easy Ways to Download Any File From Node.js Server Using Express

Getting File Extension From Paths

You can also get the file extension by providing the file path instead of a filename to the extname() method. It will return the file type correctly.

          const path = require('path');

path.extname('./public/js/main.js');
// returns .js

path.extname('./public/pic/profile.png');
// returns .png

path.extname('./pages/index.html');
// returns .html
        

You can see when I pass the paths for different files, it also returns the extensions of those files.

Getting File Extension From URLs

The extname() method can also handle URLs. If you pass an URL of a file this method will return the extension of that file.

          const path = require('path');

path.extname('https://www.example.com/js/main.js');
// returns .js

path.extname('https://www.example.com/pic/profile.png');
// returns .png

path.extname('https://www.example.com/about.html');
// returns .html
        

It is working exactly how it suppose to with the URLs. It extracts the last past from the URL and returns that as the file type.

How to Get File Extensions in Node.js Using parse() Method

The parse() method is available in the path module. To use this method you have to import the path module from NodeJS. It accepts a string as its parameter.

You can pass a filename, path, or URL to this method. After you call it with the appropriate parameter, you will be able to access properties on the method. To get the file extension you have to use the ext property.

You can also get the filename without the extension by accessing the name property after calling the parse() method.

Also Read: How to Get All Files in Directories (Recursively) with Node.js

Getting File Extension From Filenames

To get the file extension from a filename, you have to call the parse() method with the filename as its argument. But first, import the path module to access this method.

          const path = require('path');

path.parse('main.js').ext;
// returns .js

path.parse('profile.png').ext;
// returns .png

path.parse('about.html').ext;
// returns .html
        

In this example, I am calling the parse() method with the filename and accessing the ext property to obtain the file extension.

You can see this technique returning the extension names correctly. It also works fine with any files. This method extracts the file extension by tracking the (.) character in the given string.

          const path = require('path');

path.parse('profile').ext;
// returns '' (empty string)

path.parse('.profile').ext;
// returns '' (empty string)

path.parse('profile.').ext;
// returns .

path.parse('.profile.png').ext;
// returns .png
        
  • If the string doesn't have any (.) character or has only one at the beginning, this method will return an empty string.
  • When the string contains one (.) character at the end of the string, this method will return the (.) character.
  • If the string contains multiple (.) characters, this method will give the part available after the last (.) character.

The parse() method will throw a TypeError when you pass an argument that is not a string. So, you must provide an argument to it as a string.

Getting File Extension From Paths

You can find out the file extension by using a file path instead of a filename. For that, you just have to change the argument. Everything else will be the same as you have seen in the previous section.

When you pass a path for the file in the parse() method, it will also return the extension from that string.

          const path = require('path');

path.parse('./public/js/main.js').ext;
// returns .js

path.parse('./public/img/profile.png').ext;
// returns .png

path.parse('./pages/about.html').ext;
// returns .html
        

Also Read: Best Guide to Download File Using Node.js (HTTPS Module)

Getting File Extension From URLs

You can use file URLs in the parse() method to get the file extensions. This method can return the extension name from an URL because the URL will contain the filename at the end of it.

          const path = require('path');

path.parse('https://www.example.com/js/main.js').ext;
// returns .js

path.parse('https://www.example.com/img/profile.png').ext;
// returns .png

path.parse('https://www.example.com/about.html').ext;
// returns .html
        

How to Get File Extensions From MIME Type in Node.js

Sometimes you might want to find out the file extensions using the MIME types instead of the filenames. When you upload files in the Node.js application using 3rd party packages, you will receive information about the uploaded files.

Those packages also provide the MIME type of upload files. In this situation, you can use the MIME type of a file to get the file extension.

To use this technique, you have to install the mime-types package in your application. You can install this package by using the following command:

          npm install --save mime-types
        

After installing the package, you can import it into your file. You have to call the extension() method available in this package to extract the file extension name. This method accepts one parameter as a string.

          const mime = require('mime-types');

mime.extension('text/plain');
// txt

mime.extension('image/png');
// png

mime.extension('images/png');
// false

mime.extension('application/pdf');
// pdf

        

When you call the extension() method and pass the MIME type to it as the argument, it will return the file extension.

In this example, I am passing MIME types for different files. It is also returning the file extensions correctly.

Note: If you provide incorrent MIME type to the extension() method, it will return false instead of throwing error.

Conclusion

By using the Node.js core module, you can easily extract file extensions. You have learned about the extname() and parse() method from the path module. You can use filenames, paths, or URLs with these methods.

You can also use mime-types 3rd party package to get file types using MIME types. You can apply any of these methods according to your requirements.

I hope this article was helpful and you will be able to get the file extensions in a Node.js application for any files.

Related Posts