Skip to content

Instantly share code, notes, and snippets.

@azu

azu/module.md

Last active Jan 6, 2021
Embed
What would you like to do?
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