mime-types is a JavaScript utility library for working with content-types, offering functions to look up MIME types, file extensions, and associated charsets. Currently at stable version `3.0.2`, the package primarily focuses on providing accurate MIME type data sourced from `mime-db`. It differentiates itself from older `mime` modules by explicitly returning `false` for unknown types (rather than a naive fallback), avoiding instance creation (`new Mime()`), and omitting `.define()` functionality. While its core API remains compatible with `mime@1.x`, it's maintained by the `jshttp` organization and generally receives updates driven by new MIME types or minor fixes rather than frequent new features. A key aspect is its `mime-db` dependency; programmatic API changes are considered semver, but updates to the underlying MIME type data itself are not, meaning users needing strict data versioning must manage `mime-db` via package manager overrides.
npm install mime-typesVerified import paths — ran on the pinned version, not inferred.
Demonstrates core `mime-types` functionalities: `lookup` for MIME types, `contentType` for full headers, `extension` for default extensions, and `charset` for implied charsets, including handling unknown types.
Upgrade your Node.js environment to version 18 or higher.
Always check the return value of `mime.lookup()`. Use a logical OR (`||`) to provide a fallback, e.g., `const type = mime.lookup('unrecognized') || 'application/octet-stream';`.If strict control over MIME type data versions is required, you must explicitly pin or override the `mime-db` dependency version in your project's `package.json` using your package manager's specific override syntax.
Avoid using `new Mime()` or `.define()`. If you need to add custom MIME types, contribute them to the upstream `mime-db` project. For runtime overrides, consider libraries that explicitly support definition or manage a custom mapping.
In an ESM file, use `import mime from 'mime-types';`. If you must use `require()`, ensure your file is a CommonJS module (e.g., `.cjs` extension or no `"type": "module"` in `package.json`).
Import the entire module as a default, then access its properties: `import mime from 'mime-types'; const type = mime.lookup('file.txt');`Ensure you handle the `false` return value from `mime.lookup()` explicitly, typically by providing a default fallback like `const type = mime.lookup(filename) || 'application/octet-stream';`.