A Node.js and zero-dependencies MIME type utility.
Table of Contents
Presentation
This library is powered by mime-db data. It provides a very simple, lightweight, safe yet speed utility to deal with MIME types and file extensions.
Supports 1163 extensions and 947 MIME types.
Installation
npm install node-mime-types
npm i -S node-mime-types
Technical information
Node.js
- Language: JavaScript ES6/ES7
- VM: Node.js >= Carbon (8.17.0)
Tests
Node.js >= Dubnium (10.22.1) could be required for some testing modules.
Command to run all tests:
npm test
Linting
ESLint with Airbnb base rules. See Airbnb JavaScript Style Guide.
npm run test:lint
Unit
Mocha and Chai.
npm run test:unit
Usage
Import module
const mime = require('node-mime-types');
const {
getMIMEType,
getExtension,
} = require('node-mime-types');
node-mime-types module exports an object of functions. You'll find the complete list of functions below.
mime
<Object> with the following functions.
getMIMEType(filenameOrPath)
Returns the MIME type based on the file name or path extension.
Note:
- if a file name or path is not a string or is the empty string, the empty string is returned;
- if a file name or path has no extension or an unknown extension, a default MIME type is returned;
- supports extensions in lower and upper case.
filenameOrPath
<String>- Returns: <String> Default:
application/octet-stream
Examples:
getMIMEType();
getMIMEType([]);
getMIMEType('');
getMIMEType('f');
getMIMEType('file.unknown');
getMIMEType('.js');
getMIMEType('file.html');
getMIMEType('file.css');
getMIMEType('/usr/file.json');
getMIMEType('/usr/file.json.txt');
getMIMEType('C:\\file.JS');
getMIMEType('../../path/to/file-name.XML');
getMIMEType('README.md');
getExtension(mimeType)
Returns the file extension(s) based on the MIME type.
Note:
- if a MIME type does not exist or is unknown, the empty string is returned;
- if a MIME type has only one extension related to, a string is returned;
- if a MIME type corresponds to multiple extensions, an array is returned;
- supports MIME types in lower and upper case.
mimeType
<String>- Returns: <String> | <Array> Default:
''
Examples:
getExtension();
getExtension(false);
getExtension('');
getExtension('application/unknown');
getExtension('application/json5');
getExtension('application/rtf');
getExtension('text/plain');
getExtension('application/json');
getExtension('IMAGE/PNG');
Code of Conduct
This project has a Code of Conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
Contributing
If you find any MIME type or file extension missing, please directly contribute to mime-db.
Please take also a moment to read our Contributing Guidelines if you haven't yet done so.
Support
Please see our Support page if you have any questions or for any help needed.
Security
For any security concerns or issues, please visit our Security Policy page.
Licence
MIT.