Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Created November 23, 2021 10:52
Show Gist options
  • Save tjunghans/b79ed7d59b2429358adb2214a3743951 to your computer and use it in GitHub Desktop.
Save tjunghans/b79ed7d59b2429358adb2214a3743951 to your computer and use it in GitHub Desktop.
ESM Default exports

This is something a think about a lot. I a while ago I browsed the Pure ESM package gist by Sindresorhus and there's one point When should I use a default export or named exports?.

He suggests:

My general rule is that if something exports a single main thing, it should be a default export.

There's also this:

Why use default exports at all? Simplify usage: Having a default export simplifies import when the person importing the module just wants the obvious thing from there. There’s simply less syntax and typing to do. Signal intent: A default export communicates the module author’s understanding about what the primary export is from their module.

From here

I gave this pattern a try in the DTB FXCPN source and this is my conclusion so far:

  • simpler imports
  • easy to rename imports to avoid name clash
  • auto completion in vscode for imports does not work for defaults. Could be a poor config on my setup, but I expect Vscode to suggest the default name.
  • when index.ts files are used, you don't have a default import. So there's a different import syntax when importing the same, let's say, function directly from it's file or from the sibling index.ts file.

For me the only real annoyance is the vsocde completion. I like the idea of showing what the main export is in a file with multiple exports and I also try and keep it to one export per file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment