Default vs. Named Export. What Is The Difference? (2024)

Back in the days when JavaScript programs were small pieces of scripts that added a little interactivity to websites, there was no need to break the code into pieces.

Today, JavaScript applications are rapidly growing in size, prompting programmers to consider providing mechanisms for extracting the code into separate files that can be imported on demand, making the code more maintainable and reusable.

This is exactly why modules were introduced in ES6 (2015).

Module In JavaScript

What is a module? A module is just a file.

Take a look at the trivial example of two modules (user and project):

index.htmlindex.jsmodules/ user.js project.js

The user module encapsulates the logic related to the user, like the retrieval and manipulation of the data, the project module manipulates projects.

Module file extension

If you are familiar with modules in JavaScript, you may have noticed that some resources recommend .mjs extension over the .js.

On the Web, the extension does not matter, as long as modules are served with JS MIME type - text/javascript.

But the use of the .mjs extension brings some advantages:

  • It is crystal clear that the file is a module
  • It ensures that your files are parsed as modules by Node.js and Babel

For the sake of simplicity, we will stick with the .js extension in this article.

Named Export

After adding first modules containing some functions to an application, there is a need to have a reliable way of accessing them.

First of all, the functions we should have access to should be exported from the file.

The easiest way to export function is to simply add export in front of it:

// user.jsexport const fetchUsers = () => { ...};

Basically everything can be exported - functions, var, let, const and event classes.

The only thing we should remember is that the exported items must be top-level items, you can not export anything within the function, for example.

We can export as many values as we want from a single file:

// user.jsexport const fetchUsers = () => { ...};export const transformUsers = () => { ...}; export const getAdminUsers = () => { ...};

A more convenient way is to combine all exports at the end of the file:

// user.jsconst fetchUsers = () => { ...};const transformUsers = () => { ...}; const getAdminUsers = () => { ...}; export { fetchUsers, transformUsers, getAdminUsers };

After the functions have been exported, you must import them in order to use.

The easiest way is to use the import keyword followed by a list of comma-separated functions that you want to import.

import { fetchUsers, transformUsers, getAdminUsers } from "./users";

Sometimes a module can export dozens of functions and you can import them all by using an asterisk (*):

import * as userFunctions from "./users";// All exported functions are available under "userFunctions"// Example: "userFunctions.fetchUsers()"

This type of import creates a module object that grabs all exports and makes them available as members of a userFunctions object.

In this chapter we used named exports - each value in the module has been referred by its name, and this name was also used to refer to it during import.

For named exports there is no limit to the number of values exported from a single file.

Default Export

There is another type of export that is only allowed once per file - default export:

// project.jsexport default () => { ...};

Or alternative syntax:

// project.jsconst fetchProjects = () => { ...}; export default fetchProjects;

To import the function that was exported using the default export:

// The naming is completely independent// We can use any name we likeimport fetchProjects from "./projects";// ORimport anyNameWeLike from "./projects";

For default export, it is possible to export only a single value per file.

Combining Both Approaches

It is also possible to combine both approaches in the single file:

// user.jsconst fetchUsers = () => { ...};export const transformUsers = () => { ...}; export const getAdminUsers = () => { ...}; export { transformUsers, getAdminUsers };export default fetchUsers;

Import statement:

import fetchUsers, { transformUsers, getAdminUsers } from "./users";

Renaming Imported Function

Sometimes, it can happen that the name of the imported function may already be used in your code:

import { isObject } from "./lodash";// SyntaxError: Identifier "isObject" has already been declaredconst isObject = () => { ...};

The solution is to rename the import by using as keyword followed by the new function name:

import { isObject as lodashIsObject } from "./lodash";// OKconst isObject = () => { ...};

Important note: exports can be renamed as well:

export { function1 as fn1, function2 as fn2 };

Summary

In summary, named exports are used to export multiple values.

During the import, it will be possible to use the same name to refer to the exported value.

Default exports are used to export a single value from the file.

During the import, the name of the value can be different from the exported one.

It is possible to combine both approaches in a single file.

Default vs. Named Export. What Is The Difference? (2024)

FAQs

Default vs. Named Export. What Is The Difference? ›

In summary, named exports are useful when you want to export multiple values and import them with their specific names, while default exports are handy for exporting a single value and giving it a custom name when importing. The choice between the two depends on the structure and requirements of your codebase.

What is the difference between default export and name export? ›

Named exports are useful when you have multiple entities to export, while default exports are suitable for a single, primary entity. By mastering these export techniques, you can efficiently structure your code and make it more readable and reusable.

What is the difference between default and named imports? ›

The main differences

Default exports allow us to import the exported value with any name in the importing module, while named exports require us to use the exact exported names when importing. We can have only one default export per module, but we can have multiple named exports.

What is the purpose of export default? ›

Default exports are useful to export only a single object, function, variable. During the import, we can use any name to import. Example: In this example, we are exporting the variable by using “export default” keywords.

Is using named and default exports together? ›

It's important to note that a file can have both named and default exports together. This means you can export one main thing using export default , while also exporting multiple additional values using export .

Should I use export or export default? ›

In summary, named exports are useful when you want to export multiple values and import them with their specific names, while default exports are handy for exporting a single value and giving it a custom name when importing. The choice between the two depends on the structure and requirements of your codebase.

Why avoid default exports? ›

Default exports can be imported using any name you choose. This can lead to inconsistencies when multiple engineers are working on the same codebase and using different import names.

Can you have multiple default exports? ›

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

What are named and default exports in node js? ›

The key difference between named exports and default exports lies in their usage. Named exports are useful when a module exports multiple things, like objects or functions. On the other hand, default exports are used when a module exports a single thing, like a function or a class.

Does not provide an export named default.? ›

The error 'does not provide an export named default' means there is an attempt to import a default export from a JavaScript module that doesn't have one. Verify the module's export statements and adjust the import statement accordingly, ensuring to use named or wildcard imports as needed.

What are the different types of exporting? ›

The two main types of exporting are direct and indirect exporting. Direct exporting is a type of exporting where the company directly sells products to overseas customers. Indirect exporting is a type of exporting practiced by companies that sell products to other countries with the help of an intermediary.

Why do we write export default in react? ›

The default export is also used for exporting components, values, classes, etc. in React JS, but only one element can be exported to another component at a time as a default export. In other words, a file can have only one default export.

What is an export example? ›

Some export examples are final goods like cars, cell phones, computers, or clothing. These are goods that are made in one nation from start to finish and the completed product is exported to other countries.

Is using named and default exports together consumers of your bundle will have to use? ›

Using named and default exports together. Consumers of your bundle will have to use `MyLib. default` to access the default export, which may not be what you want.

What is named imports and default imports? ›

When you write a default import, you can put any name you want after import . For example, you could write import Banana from './Button.js' instead and it would still provide you with the same default export. In contrast, with named imports, the name has to match on both sides. That's why they are called named imports!

What is no export named found in AWS? ›

You can receive the "No Export named XYX found" error in the following scenarios: You're using Fn::ImportValue in nested stacks. The exported value isn't in the same AWS Region or same AWS account where you're importing the value. The exported value isn't created or published before the stack imports it.

What are the three types of export? ›

To better understand the different scenarios, here we quote and separate the three types of exports that an exporter can fit in. They are: direct, indirect and consortium exports.

What are the different types of export in react? ›

There are two primary ways to export values with JavaScript: default exports and named exports. So far, our examples have only used default exports. But you can use one or both of them in the same file. A file can have no more than one default export, but it can have as many named exports as you like.

What does export default mean in React? ›

The default export is also used for exporting components, values, classes, etc. in React JS, but only one element can be exported to another component at a time as a default export. In other words, a file can have only one default export.

What are the different types of export in JavaScript? ›

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 6477

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.