Write a FAQ entry for `export =` vs `export default` / `import ` variants
See original GitHub issuePeople don’t understand the difference between these things:
export default class Foo {}
/* or */
class Foo {}
export = Foo;
/* or */
export class Foo { }
People also don’t understand the difference between these things:
import x = require('y');
import x from 'y';
import { x } from 'y'
import * as x from 'y';
We need to write a comprehensive answer that explains which import statements correctly acquire which exports, and also describe auto-lifting behavior present in some loaders.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:253
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Named Export vs Default Export in ES6 | by Alankar Anand
Named exports are useful to export several values. During the import, one will be able to use the same name to refer to...
Read more >16. Modules - Exploring JS
There are two kinds of exports: named exports (several per module) and default exports (one per module). As explained later, it is possible...
Read more >Export and Import - The Modern JavaScript Tutorial
Export and import directives have several syntax variants. In the previous article we saw a simple use, now let's explore more examples.
Read more >export - JavaScript - MDN Web Docs
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but...
Read more >NgModule FAQ - Angular
An NgModule can export a combination of its own declarations, selected imported classes, and imported NgModules. Don't bother re-exporting pure service modules.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
export default … (Default Export)
import … from “module”;
Notes:
import * as Calculator from "./calculator";
and then instantiating it usingnew Calculator.default()
.export = …
import … = require(“module”);
Notes:
export … (Named Export)
import { … } from “module”;
Notes:
TL;DR:
export =
orimport = require
implies the module was written as CommonJS,export default
implies it’s been authored as an ECMAScript module.export =
andimport x = require('./x')
corresponds to CommonJS/AMD/etc.'s notion ofmodule.exports
:A lot of the time, people will have exactly one thing that they’re exporting, like a function:
export default
is a construct in ES2015 modules that is supposed to give an easy way to say “hey, here’s the main thing you import from this module”. ES2015 gives it a special syntax.But it’s just special syntax; really,
default
is like any member on an ES2015 module record.b.js
could have been written like the following: