Why you should not use export default in JavaScript?
Many of us think when we have only one thing to export from a file we should use export default
and if there is more than one thing then use named export
alone or use it with export default
.
And it is totally fine to use export default
for small projects, But if you are working on a large project Or working on a project with a team then using export default
might not be a good choice.
Let us understand, Why?
❓ Why we should not use export default
The first reason for not using export default
is that While using import
we can change the name of the exported thing. Or to say, we have the option to choose a name for default exports
Like, if we have a default export like this
// helloWorld.js
export default function helloWorld() {
console.log("Hello! World");
}
So, at the time of import
, we can use any name
import HelloWorld from "helloWorld.js";
import HWorld from "helloWorld.js";
Both are correct. And both will import the helloWorld()
function.
So, if we are working with a team changing names may cause a lot of problems with the debugging of the code or with the refactoring.