JavaScript's default exports are a footgun

Published on in JavaScript

It's too easy to accidentally import a default export with a misleading name.


  • What I have wanted:
    import useSWR from 'swr'
    // or
    import useSWRImmutable from 'swr/immutable'
  • What I have accidentally done (more than once):
    import useSWRImmutable from 'swr'
    // or
    import useSWR from 'swr/immutable'
    Then I have wasted time being confused by SWR not working like I expect it to.

Named exports would prevent such silly mistakes:

// OK:
import { useSWR } from 'swr'
import { useSWRImmutable } from 'swr/immutable'

// Error:
import { useSWRImmutable } from 'swr'
import { useSWR } from 'swr/immutable'

This is why I think it's bad to use default exports (unless you have a good reason).

Besides, default exports have other issues as well.