A module cannot have multiple default exports Error [Fixed] | bobbyhadz (2024)

A module cannot have multiple default exports Error [Fixed]

# A module cannot have multiple default exports Error

The error "A module cannot have multiple default exports" occurs for 2reasons - having more than 1 default export in a file or having a glitch in yourIDE.

To solve the error, replace the second default export with a named export andreload your IDE if necessary.

If you don't have multiple default exports in the file, try reloading your IDE. WebStorm and VSCode sometimes need a reboot.

Here is an example of how the error occurs.

index.ts

Copied!

const a = 'bobby';// ⛔️ Error: A module cannot have multiple default exports.ts(2528)export default a; // πŸ‘ˆοΈ default exportconst b = 'hadz';export default b; // πŸ‘ˆοΈ default export

We can only have a single default export per file, so we have to move the seconddefault export to another file or convert it to a named export.

# Convert the second default export to a named export

Here is how you would convert the second export to a named export.

index.ts

Copied!

const a = 'bobby';// πŸ‘‡οΈ default exportexport default a;// πŸ‘‡οΈ named exportexport const b = 'hadz';

A module cannot have multiple default exports Error [Fixed] | bobbyhadz (1)

Here is how you would import the variables into another file.

another-file.ts

Copied!

// πŸ‘‡οΈ default and named importimport a, { b } from './index';console.log(a); // πŸ‘‰οΈ "bobby"console.log(b); // πŸ‘‰οΈ "hadz"

A module cannot have multiple default exports Error [Fixed] | bobbyhadz (2)

We had to wrap the named import in curly braces. You can have only one defaultexport per file, but you can have as many named exports as necessary.

If you don't want to use a named export, move the second variable to aseparate file and make sure to stick to a maximum of 1 default export perfile.

In my experience, most real-world codebases exclusively use named exports and imports because they make it easier to leverage your IDE for auto-completion and auto-imports.

You also don't have to think about which members are exported with a defaultor a named export.

# Only using named exports

Here is how you would convert the example above to use only named exports andimports.

index.ts

Copied!

// πŸ‘‡οΈ named exportexport const a = 'bobby';// πŸ‘‡οΈ named exportexport const b = 'hadz';

And here is how you would import the named exports.

index.ts

Copied!

// πŸ‘‡οΈ named importsimport { a, b } from './index';console.log(a); // πŸ‘‰οΈ "bobby"console.log(b); // πŸ‘‰οΈ "hadz"

A module cannot have multiple default exports Error [Fixed] | bobbyhadz (3)

This is much better than having to remember which values you exported asdefault, and which you exported as named.

The less you have to think about implementation details, the more you can focuson domain-specific logic in your application.

# Restart your IDE

If none of the suggestions helped and you don't have multiple default exports ina single file, restart your code editor and your development server.

# Additional Resources

You can learn more about the related topics by checking out the followingtutorials:

A module cannot have multiple default exports Error [Fixed] | bobbyhadz (2024)
Top Articles
How to Set Up and Use Binance API with Python
What is Geographic Pricing & Why Use it?
Creepshotorg
It’s Time to Answer Your Questions About Super Bowl LVII (Published 2023)
Nybe Business Id
4-Hour Private ATV Riding Experience in Adirondacks 2024 on Cool Destinations
Le Blanc Los Cabos - Los Cabos – Le Blanc Spa Resort Adults-Only All Inclusive
Room Background For Zepeto
Professor Qwertyson
Es.cvs.com/Otchs/Devoted
Brgeneral Patient Portal
Osrs Blessed Axe
Slag bij Plataeae tussen de Grieken en de Perzen
Things To Do In Atlanta Tomorrow Night
Think Up Elar Level 5 Answer Key Pdf
Craigslist Deming
Flower Mound Clavicle Trauma
Aberration Surface Entrances
Best Forensic Pathology Careers + Salary Outlook | HealthGrad
Webcentral Cuny
24 Hour Drive Thru Car Wash Near Me
St. Petersburg, FL - Bombay. Meet Malia a Pet for Adoption - AdoptaPet.com
Nevermore: What Doesn't Kill
Helpers Needed At Once Bug Fables
Student Portal Stvt
Calvin Coolidge: Life in Brief | Miller Center
What Is The Lineup For Nascar Race Today
Autotrader Bmw X5
Workboy Kennel
Rust Belt Revival Auctions
#scandalous stars | astrognossienne
Keeper Of The Lost Cities Series - Shannon Messenger
How to Draw a Sailboat: 7 Steps (with Pictures) - wikiHow
Has any non-Muslim here who read the Quran and unironically ENJOYED it?
Citibank Branch Locations In Orlando Florida
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
The Conners Season 5 Wiki
The Attleboro Sun Chronicle Obituaries
The power of the NFL, its data, and the shift to CTV
Yale College Confidential 2027
The Horn Of Plenty Figgerits
Embry Riddle Prescott Academic Calendar
Professors Helpers Abbreviation
15 Best Places to Visit in the Northeast During Summer
How to Connect Jabra Earbuds to an iPhone | Decortweaks
Gander Mountain Mastercard Login
Race Deepwoken
Enter The Gungeon Gunther
Www.homedepot .Com
Craigs List Sarasota
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5287

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.