Sub topic 10
Generating a Documentation Site with jsdoc
The comments you have written so far help people reading the source. The jsdoc command-line tool can also turn those same comments into a browsable HTML site. This chapter covers installing the tool, running it, configuring it, and fixing the problems people hit most often.
This chapter assumes you can run commands with npm and npx, and that your project keeps its JavaScript under a src folder that already has JSDoc comments.
How the tool works
The jsdoc tool parses each JavaScript file and collects every /** ... */ block attached to a symbol. It builds an internal list of "doclets", one per documented symbol. A template then renders that list as HTML pages. The default template produces one page per class or module and an index page.
Running it from the command line
Install jsdoc as a development dependency, then point it at your source folder:
npm install --save-dev jsdoc
npx jsdoc src -r -d docsThese are the options used in that command:
| Option | Meaning |
|---|---|
| `src` | The file or folder to read |
| `-r` | Recurse into subdirectories |
| `-d docs` | Write output to `docs` (the default folder is `out`) |
Open docs/index.html in a browser to see the result. Add -R README.md to put your README on the home page.
Using a configuration file
Long command lines are easy to get wrong, so most projects keep the settings in a jsdoc.json file:
{
"source": {
"include": ["src"],
"includePattern": ".+\\.js$",
"excludePattern": "(^|\\/|\\\\)_"
},
"opts": {
"destination": "docs",
"recurse": true,
"readme": "README.md"
},
"plugins": ["plugins/markdown"]
}The plugins/markdown plugin ships with jsdoc. It renders Markdown inside your descriptions, so backticks and lists in your comments show up formatted. The excludePattern shown here matches the default, which skips files and folders whose names start with an underscore.
Add an npm script so everyone generates docs the same way:
{
"scripts": {
"docs": "jsdoc -c jsdoc.json"
}
}After that, npm run docs rebuilds the site.
What appears in the output
jsdoc only shows what it can see as documented. Keep these rules in mind:
- A comment that opens with
/*instead of/**is ignored. - Symbols with no doc comment are left out of the output.
- Symbols tagged
@privateare hidden unless you pass-p. - Each ES module file needs a
@moduletag at the top, or its exports can be scattered or missing.
/**
* Price helpers for the checkout flow.
* @module pricing
*/
/**
* Calculates a price including tax.
* @param {number} price - The base price.
* @param {number} [taxRate=0.2] - Tax rate as a fraction.
* @returns {number} The price with tax applied.
*/
export function withTax(price, taxRate = 0.2) {
return price * (1 + taxRate);
}With the @module tag in place, withTax is listed on a "pricing" module page.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Only some files appear | Missing `-r` or `"recurse": true` | Turn recursion on |
| Empty module page | No `@module` tag in the file | Add one at the top of the file |
| A function is missing | Comment starts with `/*` | Use `/**` |
| "There are no input files" | Wrong path or `includePattern` | Check `source.include` |
| `docs` folder committed by accident | Generated output in version control | Add `docs/` to `.gitignore`, or publish it on purpose |
Exercise
You run npx jsdoc src -d docs and the generated site includes src/index.js but none of the files in src/utils/. What change fixes this, both on the command line and in jsdoc.json?
Show answer
Add recursion. On the command line, add -r (npx jsdoc src -r -d docs). In jsdoc.json, set "recurse": true inside "opts".
Exercise
A file src/_internal.js has complete JSDoc comments but never shows up in the output. Why?
Show answer
The default excludePattern, (^|\/|\\)_, skips files and folders whose names start with an underscore. Rename the file, or change excludePattern if you want it included.