How can I alias a default import in JavaScript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In JavaScript modules, a default import is already locally named by the importing file. That means “aliasing” a default import is usually just a matter of choosing the local name you want in the normal import X from "module" syntax.
Default Imports Are Already Renameable
Suppose a module exports a default value:
When you import that module, you do not have to use the original function name. You can choose any local identifier:
That is the standard way to “alias” a default import. The name addNumbers exists only in the importing module. It does not change the exported symbol in mathUtils.js.
Why This Is Different from Named Imports
Named imports behave differently because the exported name matters:
To rename a named import, you do need as:
That is where the confusion usually comes from. People remember named-import aliasing and assume default imports need the same syntax. They do not.
The Less Common Equivalent Form
JavaScript also allows this:
This works, but it is rarely the clearest choice. For default imports, most codebases prefer:
The shorter version communicates the same thing more directly.
A Practical Example
Default-import renaming is useful when the exported name is too generic for the current file.
In another file:
Here the exported function might be called run, but startServer makes more sense in the local context.
Mixing Default and Named Imports
You can import a default export and named exports from the same module in one statement:
Then:
Again, fetchResource is just the local name you chose for the default export.
When Renaming Helps
Picking a different local name is useful when:
- two imports would otherwise collide
- the default export name is too vague
- the current file needs domain-specific clarity
- team naming conventions differ from the exported file
The important point is that the import name is a local binding, not a global rename.
Common Pitfalls
The most common mistake is trying to use named-import syntax for a default export:
That fails if add was exported as default rather than as a named export.
Another common confusion is thinking the exported function name inside the original file controls the import name. It does not. For default exports, the importer chooses the local name.
A third issue is overusing the longer default as form when the simpler default-import syntax is clearer.
Summary
- A default import can already be named however you want locally.
- The normal syntax is
import MyName from "./module.js". - '
asis mainly needed for named imports, not ordinary default imports.' - '
import { default as X }works, but it is usually less readable.' - Default-import renaming only changes the local binding in the current file.
Related reading
- How can I automatically start a node.js application in Amazon Linux AMI on aws?
- How can I await an event listener inside a function?
- How can I capitalize the first letter of each word in a string?
- How can I center an absolutely positioned element in a div?
- How can I center text (horizontally and vertically) inside a div block?
- How can I change the color of an 'svg' element?
- How can I compare software version number using JavaScript? only numbers
- How can I construct a tree using d3 and its force layout?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.