mime vs mime-types
MIME Type Libraries Comparison
1 Year
mimemime-typesSimilar Packages:
What's MIME Type Libraries?

MIME type libraries are essential in web development for handling the types of files being served over the internet. They help in determining the correct content type for files, which is crucial for proper rendering and handling by web browsers and applications. The 'mime' and 'mime-types' packages serve this purpose but differ in their approaches and features. Understanding these differences can help developers choose the right library for their specific needs, ensuring efficient file type handling in their applications.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
mime72,026,1162,239108 kB012 days agoMIT
mime-types69,498,5721,37622.3 kB1220 days agoMIT
Feature Comparison: mime vs mime-types

MIME Type Lookup

  • mime:

    The 'mime' package provides a simple and efficient way to look up MIME types based on file extensions. It uses a straightforward mapping of extensions to their corresponding MIME types, making it quick to retrieve the type for common file formats without additional overhead.

  • mime-types:

    The 'mime-types' package offers an extensive list of MIME types and supports both file extensions and MIME type lookups. It allows for more complex queries, such as retrieving all extensions associated with a specific MIME type, making it suitable for applications that require comprehensive MIME type management.

Performance

  • mime:

    The 'mime' library is optimized for performance with a minimal footprint, making it ideal for applications where speed is critical. Its lightweight nature ensures that lookups are fast and efficient, which is beneficial for high-traffic applications.

  • mime-types:

    While 'mime-types' is slightly heavier due to its extensive features, it is still performant for most applications. However, the additional functionality may introduce slight overhead compared to the 'mime' library, which could be a consideration for performance-sensitive applications.

Data Structure

  • mime:

    The 'mime' package utilizes a simple object structure for storing MIME types, which allows for quick lookups and easy maintenance. This simplicity makes it easy for developers to understand and modify the library if necessary.

  • mime-types:

    The 'mime-types' package employs a more complex data structure that includes arrays and objects to manage the relationships between MIME types and file extensions. This structure supports advanced features but may require a deeper understanding of the library's internals.

Community and Maintenance

  • mime:

    The 'mime' package has a smaller community and fewer contributors, which may affect the speed of updates and support. However, it is still actively maintained and suitable for projects that do not require frequent changes.

  • mime-types:

    The 'mime-types' package benefits from a larger community and more contributors, leading to more frequent updates and a broader range of support. This makes it a more reliable choice for long-term projects that may need ongoing maintenance.

Use Cases

  • mime:

    The 'mime' package is best suited for smaller applications or scripts where basic MIME type functionality is needed without the complexity of additional features. It is ideal for quick projects or utilities that require minimal setup.

  • mime-types:

    The 'mime-types' package is designed for larger applications that need robust MIME type handling, such as web servers or applications that serve a wide variety of file types. It is suitable for projects that require detailed MIME type management and flexibility.

How to Choose: mime vs mime-types
  • mime:

    Choose 'mime' if you need a lightweight library that focuses on basic MIME type lookups and is easy to integrate into smaller projects. It is suitable for applications that require minimal overhead and straightforward MIME type handling.

  • mime-types:

    Choose 'mime-types' if you require a more comprehensive solution with additional features such as support for file extensions and a more extensive list of MIME types. This package is ideal for larger applications where robust MIME type management is necessary.

README for mime

Mime

NPM downloads Mime CI

An API for MIME type information.

  • All mime-db types
  • Compact and dependency-free mime's badge
  • Full TS support

[!Note] mime@4 is now latest. If you're upgrading from mime@3, note the following:

  • mime@4 is API-compatible with mime@3, with ~~one~~ two exceptions:
    • Direct imports of mime properties no longer supported
    • mime.define() cannot be called on the default mime object
  • ESM module support is required. ESM Module FAQ.
  • Requires an ES2020 or newer runtime
  • Built-in Typescript types (@types/mime no longer needed)

Installation

npm install mime

Quick Start

For the full version (800+ MIME types, 1,000+ extensions):

import mime from 'mime';

mime.getType('txt');                    // ⇨ 'text/plain'
mime.getExtension('text/plain');        // ⇨ 'txt'

Lite Version mime/lite's badge

mime/lite is a drop-in mime replacement, stripped of unofficial ("prs.*", "x-*", "vnd.*") types:

import mime from 'mime/lite';

API

mime.getType(pathOrExtension)

Get mime type for the given file path or extension. E.g.

mime.getType('js');             // ⇨ 'text/javascript'
mime.getType('json');           // ⇨ 'application/json'

mime.getType('txt');            // ⇨ 'text/plain'
mime.getType('dir/text.txt');   // ⇨ 'text/plain'
mime.getType('dir\\text.txt');  // ⇨ 'text/plain'
mime.getType('.text.txt');      // ⇨ 'text/plain'
mime.getType('.txt');           // ⇨ 'text/plain'

null is returned in cases where an extension is not detected or recognized

mime.getType('foo/txt');        // ⇨ null
mime.getType('bogus_type');     // ⇨ null

mime.getExtension(type)

Get file extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.

mime.getExtension('text/plain');               // ⇨ 'txt'
mime.getExtension('application/json');         // ⇨ 'json'
mime.getExtension('text/html; charset=utf8');  // ⇨ 'html'

mime.getAllExtensions(type)

[!Note] New in mime@4

Get all file extensions for the given mime type.

mime.getAllExtensions('image/jpeg'); // ⇨ Set(3) { 'jpeg', 'jpg', 'jpe' }

Custom Mime instances

The default mime objects are immutable. Custom, mutable versions can be created as follows...

new Mime(type map [, type map, ...])

Create a new, custom mime instance. For example, to create a mutable version of the default mime instance:

import { Mime } from 'mime/lite';

import standardTypes from 'mime/types/standard.js';
import otherTypes from 'mime/types/other.js';

const mime = new Mime(standardTypes, otherTypes);

Each argument is passed to the define() method, below. For example new Mime(standardTypes, otherTypes) is synonomous with new Mime().define(standardTypes).define(otherTypes)

mime.define(type map [, force = false])

[!Note] Only available on custom Mime instances

Define MIME type -> extensions.

Attempting to map a type to an already-defined extension will throw unless the force argument is set to true.

mime.define({'text/x-abc': ['abc', 'abcd']});

mime.getType('abcd');            // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc')  // ⇨ 'abc'

Command Line

Extension -> type

$ mime scripts/jquery.js
text/javascript

Type -> extension

$ mime -r image/jpeg
jpeg