Skip to content

How to document an ES2015 module (single default export)

Lloyd Brookes edited this page Apr 28, 2020 · 2 revisions

1. Say you have a ES2015 module you'd like to document.

export default function add (a, b) {
  return a + b
}

2. Given that jsdoc2md only generates markdown for documented identifiers and modules, you must document each identifier you want to appear in output - including the module. Therefore, you must use @module at the top of the file to document the module.

/**
 * A module for adding two values.
 * @module add-two-values
 */

/**
 * Add two values.
 */
export default function add (a, b) {
  return a + b
}

3. This file will now appear in jsdoc2md output (without the @module tag it will not appear):

es6-modules-default

exporting a function directly

module.exports() ⏏

exported function

Kind: Exported function

4. However, under the hood jsdoc has applied the add two values documentation to the export default statement (which has been renamed to module.exports). You can avoid this by being more explicit (documenting the function directly, aliasing it as the exported value):

/**
 * A module for adding two values.
 * @module add-two-values
 */
export default add

/**
 * Add two values.
 * @alias module:add-two-values
 */
function add (a, b) {
  return a + b
}

5. That's better:

add-two-values

A module for adding two values.

add() ⏏

Add two values.

Kind: Exported function

Clone this wiki locally