Skip to main content

ES2015 Modules

Declaring a module

Add a @module tag (with no name) to let JSDoc infer the module name from the filename, or provide an explicit name:

/** @module geometry */

/**
* Calculate the area of a circle.
* @param {number} r - Radius.
* @returns {number}
*/
export function circleArea(r) {
return Math.PI * r * r;
}

Named exports

Document each exported symbol with its own JSDoc comment. JSDoc automatically recognises export declarations:

/** @module string-utils */

/**
* Capitalise the first letter of a string.
* @param {string} str
* @returns {string}
*/
export function capitalise(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

/**
* Truncate a string to the given length, appending an ellipsis.
* @param {string} str
* @param {number} maxLength
* @returns {string}
*/
export function truncate(str, maxLength) {
return str.length > maxLength ? `${str.slice(0, maxLength)}` : str;
}

Default export

/** @module Logger */

/**
* Writes messages to the console with a timestamp.
*/
class Logger {
/** @param {string} level - Log level label. */
constructor(level = 'info') {
this.level = level;
}

/** @param {string} message */
log(message) {
console.log(`[${this.level}] ${message}`);
}
}

export default Logger;

Re-exports

When re-exporting from another module, document the original symbol and note the re-export:

/**
* @module api
*/

export { default as Logger } from './logger.js';
export { capitalise, truncate } from './string-utils.js';

Referencing module members

Use the module: namepath prefix to link to a module's exports:

/**
* @param {module:geometry.circleArea} fn
*/

See also

Official reference: jsdoc.app/howto-es2015-modules