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.
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.
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
What's the difference between export function vs export default?
I use export default when exporting only one component.
If you have multiple components per file to export then use just 'export' and then on the file you want to import them you can do it this way:
import { Head, Logo, Menu } from './Header';
More on reddit.comES6 export default function - javascript
how to export and import default function - javascript
What is export default?
Videos
Here's what I mean:
export const MyFunction () => {}
and:
export default MyFunction
Is there any difference between the two in terms of recommended preference, or performance or anything? I honestly would prefer using the export in front of the function myself, but I'll go with whatever is the best practice.
I use export default when exporting only one component.
If you have multiple components per file to export then use just 'export' and then on the file you want to import them you can do it this way:
import { Head, Logo, Menu } from './Header';
The actual differences between the two have already been stated so I'll just add that I have an es/tslint rule against allowing default exports entirely. Here's a link to the rule for an explanation.
If I do use default exports because it matches the rest of the repo (usually when I'm in React-land instead of Node-land) I feel better about myself by doing:
export const Thingy = props => { ... };
export default Thingy;
madox2's answer totally works if you want to import named functions.
If you still want to import the default, there's another technique:
function a() {}
function b() {}
export default { a, b }
and when you import:
import myObject from './index.js';
myObject.a(); // function a
myObject.b(); // function b
I hope this helps!
You can use named export instead of default:
export function aFnt(){
console.log("function a");
}
export function bFnt(){
console.log("function b");
}
and import it like:
import {aFnt, bFnt} from "./index";
using CommonJS Nodejs Docs
exporting one module :
HelloLog.js :
module.exports = (str) => {
console.log(`Hello, logging ${str}!`);
}
Client.js :
const HelloLog = require('./HelloLog');
HelloLog("foobar")
using ECMAScript MDN Docs Nodejs Docs
HelloLog.js :
// Default exports choose any
export default expression;
export default function (…) { … } // also class, function*
export default function name1(…) { … } // also class, function*
export { name1 as default, … };
Client.js :
import HelloLog from './HelloLog';
HelloLog("foobar")
- CommonJS and ECMAScript can't be mixed.
Or this.
module.exports = (str) => {
console.log(`Hello, logging ${str}!`);
}
const HelloLog = require('./HelloLog');
HelloLog("foobar");