How to import default exports in JavaScript

How to import default exports in JavaScript

When you’re dealing with JavaScript modules, the syntax around importing default exports is deceptively simple but worth nailing down precisely. The key thing to remember is that a default export means the module is exporting a single value, function, or class as the “main” thing. Unlike named exports, where you wrap the imported names in curly braces, default imports come without any braces.

Here’s the most simpler way to import a default export:

import MyComponent from './MyComponent';

Notice how there are no curly braces around MyComponent. That’s because the module you’re importing from does something like this:

export default function MyComponent() {
  // component logic here
}

Or even:

const MyComponent = () => {
  // some code
};
export default MyComponent;

What you get with import MyComponent from './MyComponent' is exactly what was assigned to the default export in that module. It could be a function, a class, an object, whatever – just one thing.

One subtlety is that the name you import with doesn’t have to match the original identifier inside the module. You can rename it on the spot:

import WhateverNameIWant from './MyComponent';

The imported value will still be the default export, but now you’re referring to it as WhateverNameIWant in your own file. This can be handy if you want to avoid naming collisions or just prefer a clearer name in your context.

Default imports don’t mix with named imports inside the same curly braces. If you want both default and named exports, you have to write:

import React, { useState, useEffect } from 'react';

Here, React is the default export, and useState and useEffect are named exports. The syntax separates them clearly: default import first, then named imports inside braces.

It’s worth pointing out that default exports are a bit of a convenience feature. They let you write concise import statements, but they can also hide what exactly you’re importing if you’re not careful. That’s why, in larger codebases, some teams prefer to avoid default exports altogether to keep everything explicit.

Still, understanding the syntax and the way the JavaScript module system treats default exports lets you read and write clean, effective code without surprises.

It’s time to look at some common mistakes people make when importing defaults and how to sidestep them.

Common pitfalls and how to avoid them when importing defaults

One common pitfall is attempting to import a default export using curly braces. This will result in an error because the JavaScript engine expects a named export when it sees braces. For example, if you try to do this:

import { MyComponent } from './MyComponent';

You’ll get an error saying that MyComponent is not a named export. Remember, default exports stand alone, while named exports are always wrapped in curly braces.

Another mistake is forgetting the correct file extension when importing. JavaScript modules typically require the full path, including the file extension, unless you’re importing from a package. For instance, if your module is in a file named MyComponent.js, you should write:

import MyComponent from './MyComponent.js';

Omitting the .js could lead to unexpected behavior or errors, especially if your build system is strict about file extensions.

Watch out for case sensitivity as well. If your file is named mycomponent.js but you try to import it as MyComponent, you will face a module not found error on case-sensitive file systems. Always match the casing of your file names exactly.

In addition, be cautious when renaming imports. While it allows for flexibility, it can also lead to confusion if the new name doesn’t clearly convey the purpose of the imported module. For example:

import SomethingElse from './MyComponent';

If SomethingElse doesn’t reflect the actual functionality of MyComponent, it can mislead other developers (or your future self) trying to understand the code.

Finally, when using default exports, be careful about circular dependencies. If two modules import each other’s default exports, it can lead to unexpected behavior or runtime errors. Always try to design your module dependencies carefully to avoid these situations.

By being aware of these common pitfalls and understanding the nuances of default imports, you can write cleaner, more reliable code that’s easier to maintain and understand.

Source: https://www.jsfaq.com/how-to-import-default-exports-in-javascript/


You might also like this video