`import {getType} from 'mime'` no longer works in `mime@4`
broofa opened this issue · 1 comments
As noted by @MartinX3, directly importing properties of the mime
object - e.g. import { getType } from 'mime';
- no longer works in mime@4.
First, it's worth noting that this sort of import was technically never a documented part of the API. The README has always recommended importing and accessing the API through the mime
object. That said, I'm not surprised that some devs prefer this style of import. And judging by the code, I apparently went out of my way to support it at some point. So... fine, "we'll allow it." 😄
That said, I consider the new behavior to be "working as intended". It's a natural consequence of a couple important architectural changes in v4. Specifically:
- Object-based API. Having the
mime
API rooted in instances of aMime
class allows for a clean separation between immutable, default objects and mutable, custom objects. Thus, it makes sense to export themime
object as a whole. This is how the code was structured previously, but the new version really leans into this decision in ways it didn't used to. - Switching to ESM-only. ESM doesn't support exporting all properties of an object by design. Doing so gets awkward and is arguably a bit of a code-smell.
While maintaining backward compatibility is doable, it would require a couple of code patterns that I don't particularly care for. And since this is a major-version release I think it's appropriate to drop support at this time.
For those affected by this, my apologies. There's a couple options...
Option 1: Patch affected code (recommended)
For most users, just patch up your existing code.
// Change this:
import {getType} from 'mime';
// ... to this:
import mime from 'mime';
// ... and change calls like this:
const type = getType(someType);
// ... to this:
const type = mime.getType(someType);
Option 2: Create local var(s) bound to the mime
instance
If for some reason you have bunch of call sites that you'd rather not deal with updating (why?!?), you can do this.
// Change this:
import {getType} from 'mime';
// ... to this:
import mime from 'mime';
const getType = mime.getType.bind(mime);
Option 3: Use dynamic import()
If you happen to be dynamically require()'ing or import()'ing mime
already, you can just destructure the defualt
object. I wouldn't recommend this otherwise, however.
// Change this:
import { getType } from 'mime';
// ... to this:
const { getType } = (await import('mime')).default
Option 4: Create a helper module that binds and exports the desired properties
Lastly, if you have a bunch of code affected by this and really want to miminize your code churn I suppose you can do something like this. ('Would love to hear the use case for this, if anyone actually has one.)
// --- "mime-helper.js" ---
import mime from 'mime';
const getType = mime.getType.bind(mime);
export { getType, getExtension, getAllExtensions };
// Then, elsewhere, change this:
import { getType } from 'mime';
// ... to this:
import { getType } from './mime-helper.js';