JSDoc
JSDoc is an API documentation generator for JavaScript, enabling developers to create static HTML documentation websites directly from specially formatted comments within their source code. It supports modern JavaScript features, including ES2015 classes and modules, through its use of the Babylon (now @babel/parser) JavaScript parser. The project is actively maintained, with the current stable version being 4.0.5, and receives irregular but consistent updates focused on bug fixes, Node.js compatibility, and parser improvements. Key differentiators include its long-standing presence in the JavaScript ecosystem (since 1999), flexible configuration options via JSON or JavaScript files, and a rich community ecosystem offering numerous custom templates and build tool integrations (e.g., Grunt, Gulp). JSDoc allows for comprehensive annotation of functions, classes, modules, and parameters, facilitating improved code readability, maintainability, and providing rich IntelliSense support in modern IDEs.
Common errors
-
Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
cause Attempting to install JSDoc globally without sufficient permissions on Unix-like systems.fixUse `npm install -g jsdoc --force` or fix npm permissions using `sudo npm install -g jsdoc` (less recommended due to security implications, better to fix npm's directory permissions). Refer to npm's documentation on 'resolving EACCES permissions errors'. -
command not found: jsdoc
cause JSDoc was installed locally (e.g., `npm install --save-dev jsdoc`) but is being invoked without specifying its local path or running via an npm script.fixIf installed locally, run it using `./node_modules/.bin/jsdoc yourJavaScriptFile.js` or define an npm script in `package.json` like `"docs": "jsdoc -c jsdoc.json"` and run with `npm run docs`. -
Parsing error: Unexpected token ... (related to ES2015+ syntax)
cause Using an older version of JSDoc (prior to 3.5.0) that did not fully support modern JavaScript syntax (e.g., classes, arrow functions, async/await).fixUpgrade JSDoc to version 3.5.0 or newer. This version introduced the Babylon parser, providing robust support for contemporary JavaScript and JSX. -
JSDoc did not generate documentation for some comments.
cause Comments might not start with exactly `/**`, or are placed incorrectly, leading JSDoc to ignore them. Earlier versions also had issues with comments at the end of files or specific namepath formats.fixEnsure all desired JSDoc blocks start with `/**`. Check for extra asterisks (`/***`) or single-asterisk block comments (`/*`). Verify that comments are immediately preceding the code they document. Upgrade to JSDoc 3.5.3 or later for fixes related to comment parsing in specific scenarios (e.g., end-of-file comments, specific namepaths).
Warnings
- breaking JSDoc 3.5.0 switched to the Babylon (now @babel/parser) JavaScript parser, significantly enhancing support for modern JavaScript and JSX syntax. This change might alter how some edge cases or previously unsupported syntax were handled, potentially leading to different parsing results or requiring adjustments for custom plugins.
- breaking JSDoc 3.5.0 introduced support for using JavaScript files for configuration, alongside JSON. While JSON remains supported, JavaScript configuration offers greater flexibility but changes the expected file type if you switch.
- gotcha JSDoc requires Node.js 12.0.0 or later for version 4.x. Older versions of JSDoc had specific Node.js compatibility fixes (e.g., 3.5.5 fixed incompatibility with Node.js 8.5.0, 4.0.4 fixed with Node.js 23). Running with an unsupported Node.js version can lead to errors or unexpected behavior.
- gotcha By default, JSDoc only recognizes comments starting with `/**`. Comments beginning with `/*`, `/***`, or more than three asterisks are ignored. This is an intentional feature for suppressing parsing.
- gotcha The `@hideconstructor` tag with the default template had an issue in earlier 3.x versions, where it might not have correctly hidden the constructor documentation.
Install
-
npm install jsdoc -
yarn add jsdoc -
pnpm add jsdoc
Imports
- jsdoc
This package is primarily a command-line interface (CLI) tool and is not typically imported programmatically as a library in standard JavaScript code. Instead, it's executed via the 'jsdoc' command.
- Configuration
import config from 'jsdoc/conf'
jsdoc -c path/to/conf.json
- Doclet (internal concept)
N/A (internal data structure)
Quickstart
npm install --save-dev jsdoc
// my-module.js
/**
* Represents a simple calculator.
* @class
*/
class Calculator {
/**
* Adds two numbers.
* @param {number} a - The first number.
* @param {number} b - The second number.
* @returns {number} The sum of a and b.
*/
add(a, b) {
return a + b;
}
/**
* Subtracts two numbers.
* @param {number} x - The number to subtract from.
* @param {number} y - The number to subtract.
* @returns {number} The difference of x and y.
*/
subtract(x, y) {
return x - y;
}
}
module.exports = Calculator;
// package.json (add this script)
// {
// "scripts": {
// "generate-docs": "jsdoc my-module.js -d ./docs"
// }
// }
// To run: npm run generate-docs