# media-typer

> Simple RFC 6838 media type parser and formatter

Latest version **2.0.0** (published 2026-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install media-typer
pnpm add media-typer
yarn add media-typer
bun add media-typer
```

## Health

**Score 63/100 (C)** — status: active.

Positive: has types package; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2026-05-27 |
| First published | 2014-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/media-typer) |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 59 |
| Author | Douglas Christopher Wilson |
| Maintainers | ulisesgascon, blakeembrey, dougwilson |

## Links

- npm: https://www.npmjs.com/package/media-typer
- Repository: https://github.com/jshttp/media-typer
- Homepage: https://github.com/jshttp/media-typer#readme
- Issues: https://github.com/jshttp/media-typer/issues
- Funding: https://opencollective.com/express
- npm.io page: https://npm.io/package/media-typer

## Recent versions

- 2.0.0 (latest) — 2026-05-27
- 1.1.1 (legacy) — 2026-07-23
- 1.1.0 — 2019-04-25
- 1.0.2 — 2019-04-19
- 1.0.1 — 2018-10-21
- 1.0.0 — 2018-10-21
- 0.3.0 — 2014-09-08
- 0.2.0 — 2014-06-18
- 0.1.0 — 2014-06-18
- 0.0.0 — 2014-06-13

## README

# media-typer

[![NPM Version][npm-version-image]][npm-url]
[![NPM Downloads][npm-downloads-image]][npm-url]
[![Build Status][ci-image]][ci-url]
[![Coverage Status][coverage-image]][coverage-url]

Simple RFC 6838 media type parser.

This module will parse a given media type into its component parts, like type,
subtype, and suffix. A formatter is also provided to put them back together and
the two can be combined to normalize media types into a canonical form.

If you are looking to parse the string that represents a media type and its
parameters in HTTP (for example, the `Content-Type` header), use the
[content-type module](https://www.npmjs.com/package/content-type).

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```sh
$ npm install media-typer
```

## API

```js
import * as typer from "media-typer";
```

### typer.parse(string)

```js
const obj = typer.parse("image/svg+xml");
```

Parse a media type string. This will return an object with the following properties:

- `type`: The type of the media type (always lower case). Example: `'image'`
- `subtype`: The subtype of the media type (always lower case). Example: `'svg'`
- `suffix`: Optional suffix of the media type (always lower case). Example: `'xml'`

If the given type string is invalid, then a `TypeError` is thrown.

### typer.format(obj)

```js
const obj = typer.format({ type: "image", subtype: "svg", suffix: "xml" });
```

Format an object into a media type string. This will return a string of the
mime type for the given object. For the properties of the object, see the
documentation for `typer.parse(string)`.

If any of the given object values are invalid, then a `TypeError` is thrown.

### typer.test(string)

```js
const valid = typer.test("image/svg+xml");
```

Validate a media type string. This will return `true` if the string is a well-
formatted media type, or `false` otherwise.

## License

[MIT](LICENSE)

[ci-image]: https://img.shields.io/github/actions/workflow/status/jshttp/media-typer/ci.yml
[ci-url]: https://github.com/jshttp/media-typer/actions/workflows/ci.yml?query=branch%3Amaster
[coverage-image]: https://img.shields.io/codecov/c/github/jshttp/media-typer/master
[coverage-url]: https://app.codecov.io/gh/jshttp/media-typer
[npm-downloads-image]: https://img.shields.io/npm/dm/media-typer
[npm-url]: https://npmjs.org/package/media-typer
[npm-version-image]: https://img.shields.io/npm/v/media-typer

---
_Source: https://npm.io/package/media-typer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
