🌐
Reddit
reddit.com › r/javascript › default exports in javascript modules are terrible
r/javascript on Reddit: Default Exports in JavaScript Modules Are Terrible
September 1, 2022 - Seriously I've devoted more brain cells to responding to this comment than I ever have in dealing with any kind of "fallout" from using default exports. It's just plain not an issue in real life. It's like arguing over tabs vs spaces. While tabs are obviously superior, the correct answer is "use the one the codebase is already using." ... JavaScript was first released 26 years ago.
🌐
Reddit
reddit.com › r/reactjs › can someone explain me what does export default do?
r/reactjs on Reddit: Can someone explain me what does export default do?
November 22, 2022 -

Does it export the whole file, the whole component and everything in his scope or ? ( I am confused since I see we have the same name file as the name of the function (react component) and we use the same casing. So which one is getting targeted? Thanks.

🌐
Reddit
reddit.com › r/javascript › `export default thing` behaves differently to `export { thing as default }`
r/javascript on Reddit: `export default thing` behaves differently to `export { thing as default }`
July 6, 2021 - Yeah defaulting to default exports is a really great way to increase the frequency of breaking changes. You can just use named exports for additional exports from that module later in the day but then the module semantics are screwed and entirely ...
🌐
Reddit
reddit.com › r/javascript › is using default exports considered a bad practice? here’s why!
r/javascript on Reddit: Is using default exports considered a bad practice? Here’s why!
June 26, 2020 - So like you've said, I've resolved to exporting both and I still tend to use the named imports. ... its not bad. You use default exports when you stick to the one Class per sourcefile rule.
🌐
Reddit
reddit.com › r/javascript › [askjs] opinions on how to export default const
r/javascript on Reddit: [AskJS] Opinions on how to export default const
January 22, 2023 -

I'm looking for your thoughts on organizing exports. The problem that I'm having is that I have large const declarations that stretch like 5+ lines, and I want to know if the const is exported or not. Usually with export default function ... I can tell right away. I have toyed around with putting all exports at the top of the file, but nobody else seems to do that... I'm wondering how you would do it.

Lets say I have this example file:

import oscillate from "./oscillate.js";

export function enable() {
  document.querySelector("video").play();
}

export function disable() {
  document.querySelector("video").pause();
}

/**
 * Large doc comment
 */
// 👇 I want this exported as the default export
const toggle = oscillate(
  () => {
    console.log("From oscillate! Rising");
    enable();
  },
  () => {
    console.log("From oscillate! Falling");
    disable();
  }
);

// We actually use the toggle() function, so we can't leave it unnamed.
document.body.addEventListener("click", () => toggle())

Where should the export default statement go? I see three possibilities:

1: Put the export right after the variable declaration. This is pretty good, but the weakness is if the const declaration is long, it fails on both "is this variable exported?" and "what does this file export?"

const toggle = (
  // ...
  // ...
  // ...
);
// Either of these work. It's the same thing.
export { toggle as default };
export default toggle;

2: Put all the exports at the top. This makes it extremely easy to skim "what does this file export?", but much harder to answer "is this variable exported".

export { toggle as default, enable, disable };

function enable()...

function disable()...

// Already exported at the top!
const toggle = ...;

3: Put the export statement just before the declaration. This keeps the export and const declaration close right until you add a doc comment. In VS Code (and probably others) if you put the export after the doc comment but before the const declaration, it doesn't add it to the hover (it doesn't associate the comment with the const).

export function enable()...

export function disable()...

export { toggle as default };
/**
 * Large doc comment
 */
const toggle = ...;

The crux is that I can't export default const x = .... I'm looking for the next best thing. Since there doesn't seem to be a way to keep the const and the export default close together, I'm leaning towards option #2 to put it all at the top. There's just no way to really do it and keep the two close together. The doc comments are right above it, and the expression body itself pushes anything below it out of the vicinity.

Looking for other ideas and/or opinions. Maybe there's some JSDoc tag that I should use like @exported or something?

🌐
Reddit
reddit.com › r/javascript › [askjs] default-exports vs named-exports
r/javascript on Reddit: [AskJS] Default-exports vs named-exports
May 16, 2022 -

Can someone please shed more light on this statement from Axel Rauschmayer "Note that default-exporting objects is usually an anti-pattern (if you want to export the properties). You lose some ES6 module benefits (tree-shaking and faster access to imports)."

Link: https://medium.com/@rauschma/note-that-default-exporting-objects-is-usually-an-anti-pattern-if-you-want-to-export-the-cf674423ac38#.nibatprx3

Trying to understand when to use which one? Do you define any listing rules imposing one over the other ?

🌐
Reddit
reddit.com › r/javascript › `export default` considered harmful
r/javascript on Reddit: `export default` considered harmful
August 27, 2018 - A couple of remarks I do have, ... namedExport2} from "./module.js"; Re-exporting: Note that you can do export {default} from "./module.js"; or even rename exports export {default as nowWithName} from "./module.js"; ...
Find elsewhere
🌐
Reddit
reddit.com › r/javascript › 5 reasons you should abandon default exports
r/javascript on Reddit: 5 reasons you should abandon default exports
April 2, 2020 - I've switched to named exports (while using Airbnb style): less headaches. ... Default exports for classes only and only one class per file.
Top answer
1 of 12
708

It's part of the ES6 module system, described here. There is a helpful example in that documentation, also:

If a module defines a default export:

// foo.js
export default function() { console.log("hello!") }

then you can import that default export by omitting the curly braces:

import foo from "foo";
foo(); // hello!

Update: As of June 2015, the module system is defined in §15.2 and the export syntax in particular is defined in §15.2.3 of the ECMAScript 2015 specification.

2 of 12
271

export default is used to export a single class, function or primitive from a script file.

The export can also be written as

export default function SafeString(string) {
  this.string = string;
}

SafeString.prototype.toString = function() {
  return "" + this.string;
};

This is used to import this function in another script file

Say in app.js, you can

import SafeString from './handlebars/safe-string';

A little about export

As the name says, it's used to export functions, objects, classes or expressions from script files or modules

Utiliites.js

export function cube(x) {
  return x * x * x;
}
export const foo = Math.PI + Math.SQRT2;

This can be imported and used as

App.js

import { cube, foo } from 'Utilities';
console.log(cube(3)); // 27
console.log(foo);    // 4.555806215962888

Or

import * as utilities from 'Utilities';
console.log(utilities.cube(3)); // 27
console.log(utilities.foo);    // 4.555806215962888

When export default is used, this is much simpler. Script files just exports one thing. cube.js

export default function cube(x) {
  return x * x * x;
};

and used as App.js

import Cube from 'cube';
console.log(Cube(3)); // 27
🌐
Reddit
reddit.com › r › webdev › comments › 7j9nvy › why_we_have_banned_default_exports_in_javascript
r/webdev - Why we have banned default exports in Javascript and you should do the same
December 12, 2017 - The author's right, having your IDE suggest and auto-import named exports from other modules is a huge benefit. The benefit is more pronounced if you share our mutual preference for many small/simple modules. ... This article seems lazy. It's what, 200 words long? Default exports are handy for changing internal naming schemes without destroying your consumers.
🌐
Reddit
reddit.com › r/programming › default exports in javascript modules are terrible
r/programming on Reddit: Default Exports in JavaScript Modules Are Terrible
September 1, 2022 - export default (a: number, b: number) => a - b; // or const subtract = (a: number, b: number) => a - b; export default subtract; ... A javascript-related blog post I can totally get behind!
🌐
Reddit
reddit.com › r/javascript › using default exports makes javascript harder to read!
r/javascript on Reddit: Using default exports makes JavaScript harder to read!
October 12, 2022 - I will generally say this anywhere I see default exports: they are a blight and I eslint check for that junk out of all my projects. They’re like a crutch for certain features, but all they do is degrade editor capabilities. ... Ey JS gurus I have a weather application that I'm trying to develop in JavaScript.html.css.
🌐
DEV Community
dev.to › samyak112 › export-vs-export-default-in-js-2fbi
Export vs Export Default in JS - DEV Community
December 9, 2023 - When you see export default in a Javascript or React file, it means that the module is exporting a default value.
🌐
Reddit
reddit.com › r/reactjs › why do so many developers declare and export components this way? (example inside)
r/reactjs on Reddit: Why do so many developers declare and export components this way? (example inside)
October 15, 2023 -

The vast majority of React projects I've seen declare and export components as follows:

const Timer = (props) => {
  // component code here
}

export default Timer;

Even newly created default React project uses this in App.jsx file.

On one of the project I worked on it was prohibited to use default exports even for components. So we had:

export const Timer = (props) => {
  // code 
}

// and then import 
import { Timer } from './components/Timer"

The guy who created the style guide for the project believed that default exports are bad. But importing Timer from Timer is so stupid, isn't it? And it was not the only project I've seen using this kind of exporting components.

But my question is why I almost never see anyone using this:

export default function Timer(props) {
  // code
}

In my opinion it's much better than 2 previous options. It's short. It's clear. Maybe there are some cons I don't see?

🌐
Jakearchibald
jakearchibald.com › 2021 › export-default-thing-vs-thing-as-default
`export default thing` is different to `export { thing as default }` - JakeArchibald.com
July 3, 2021 - He said that some earlier designs of default exports were in the form export default = thing, which would have made it more obvious that thing is treated as an expression. I agree! This came to light when Dominic messaged me about circular dependencies. First we need to talk about 'hoisting': You might have encountered the age-old weird thing JavaScript does to functions: