MIME Type Lookup CLI
A Node CLI that maps a file extension to its MIME type and back to common extensions, with a fallback to octet-stream.
Project
node-mime-type-cli
Entry point: mime.mjs
Install
node --versionRun
node mime.mjs cssTest
node --test mime.test.mjsExpected output
Prints the MIME type for the given extension, for example text/css, and reverse lookup prints matching extensions.Dependencies
- node >=20
What it does
Keeps a table of common extensions and media types, normalizes the input by stripping a leading dot and lowercasing, and returns a safe fallback for unknown types so a server never emits an empty content type.
const TYPES = {
html: 'text/html', css: 'text/css', js: 'text/javascript', mjs: 'text/javascript',
json: 'application/json', xml: 'application/xml', txt: 'text/plain', csv: 'text/csv',
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif',
svg: 'image/svg+xml', webp: 'image/webp', pdf: 'application/pdf', zip: 'application/zip',
gz: 'application/gzip', mp4: 'video/mp4', mp3: 'audio/mpeg', woff2: 'font/woff2',
};
const REVERSE = Object.fromEntries(Object.entries(TYPES).map(([ext, type]) => [type, ext]));
const input = process.argv[2]?.replace(/^\./, '').toLowerCase();
if (!input) { console.error('usage: mime <extension|type> [--reverse]'); process.exit(1); }
if (process.argv.includes('--reverse')) {
console.log(REVERSE[input] ?? '(unknown)');
} else {
console.log(TYPES[input] ?? 'application/octet-stream');
}
Run and verify
node mime.mjs css
Run the test command to check the implementation: node --test mime.test.mjs.
Expected Output
Looking up an extension prints its media type on one line:
text/css
Limitations
A curated subset of common web types; it is not the full IANA registry, so niche formats fall back to application/octet-stream.