Skip to content

Instantly share code, notes, and snippets.

@azu
Last active January 6, 2021 11:47
Show Gist options
  • Save azu/09ee80c1a3127737000a to your computer and use it in GitHub Desktop.
Save azu/09ee80c1a3127737000a to your computer and use it in GitHub Desktop.
ES6 modulesのexport と CommonJS moduleのexports それぞれの使い分けってあるのかな

感覚的な話なので早く落ち着くといいなー

ES6 modulesでexportしたい

Entry pointが一つのみ

// Single default export
export default function entryPoint(){}

Entry pointが複数

// Multiple named exports
export function a(){}
export function b(){}

Caution ⚠️

  • defaultとnamed exportをまぜない

Bad

export default function entryPoint(){}
export function SomeThing(){}

Good

export function entryPoint(){}
export function SomeThing(){}

16.4.5.1 Recommendation: avoid mixing default exports and named exports
-- http://exploringjs.com/es6/ch_modules.html

CommonJS modulesでexportしたい

EntryPointが一つ

module.exports = function entryPoint(){};

EntryPointが複数

// exports object
module.exports = {
  a: function a(){},
  b: function b(){}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment