simple-mime

raw JSON →
0.1.0 verified Sat Apr 25 auth: no javascript

A minimal mime type lookup library for Node.js, providing a simple function to map file extensions to MIME types. Version 0.1.0 is the current stable release. It focuses on being lightweight and dependency-free, unlike the more feature-rich `mime` package. Ideal for small static file servers where minimalism is preferred.

error TypeError: object is not a function
cause Requiring the module without calling it returns a function, not a lookup function.
fix
Change require('simple-mime') to require('simple-mime')('application/octet-stream')
gotcha The module must be called immediately to create the lookup function; otherwise you get a function instead of a value.
fix Always call require('simple-mime')('your-fallback') to get the proper lookup function.
gotcha The fallback MIME type is required; passing no argument will result in undefined being used as fallback.
fix Always provide a fallback string like 'application/octet-stream'.
deprecated The package uses deprecated Node.js APIs (e.g., path.exists) which may cause deprecation warnings in newer Node.js versions.
fix Consider switching to the more maintained 'mime' package or updating the dependency.
npm install simple-mime
yarn add simple-mime
pnpm add simple-mime

Shows how to instantiate the mime lookup function with a fallback and query for different files.

var getMimeType = require('simple-mime')('text/plain');
console.log(getMimeType('file.html')); // 'text/html'
console.log(getMimeType('file.js'));   // 'application/javascript'
console.log(getMimeType('file.xyz'));  // 'text/plain' (fallback)