ES5 (CommonJS Modules)
Type Export Syntax Import Syntax Definition
Exports a single item
Single module.exports = const x =
value require('./file') from a file (function,
Export
object, etc.).
Exports multiple named
Multiple module.exports = const { a, b } =
{ a, b } require('./file') items as properties of an
Exports
object.
Named Shorthand to export
const { a } =
Export exports.a = value multiple values via the
require('./file')
Shortcut exports object.
ES6 (ECMAScript Modules)
Type Export Syntax Import Syntax Definition
export const x
Named = ... import { x, y } from Export variables or
Export export function './file.js' functions by name.
y() {}
Default export default Exports a single default
import x from './file.js'
Export something value per file.
Multiple import { a, b } from Group and export
export { a, b }
Exports './file.js' multiple identifiers.
Import with import { a as x } from Rename named import
— './file.js'
Rename during import.
export default
Mixed something import x, { a } from Import default and
Import export const a './file.js' named exports together.
= ...
export * from import { a } from Re-export all exports
Export All './file.js' './file.js' or re-exported file from another module.
Loads a module
Dynamic const module = await
— import('./file.js') asynchronously at
Import
runtime.
Arrow Function Export Notes: -
-Valid:
export default (a, b) => a + b; //anonymous default export
- Valid:
const add = (a, b) => a + b;
export default add; //named function exported later
- Invalid:
export default const add = (...) //Invalid syntax