How to Generate JavaScript Documentation with JSDoc 3 and PhpStorm

This guide is outdated. I wrote the new tutorial Use JSDoc 3 and PhpStorm to generate JavaScript documentation

This is an update to my previous article How to create JavaScript documentation in PhpStorm, since the JsDoc Toolkit is no longer under active development. It got replaced by JSDoc 3, with better support for current coding practices, particularly the CommonJS Modules standard and also its improved format, Asynchronous Module Definition (AMD).

This guide should not only work for Jetbrains’ PhpStorm, but any IDE, such as Eclipse or Aptana, that lets you call an external tool with parameters. It is written for Windows 7, but should also work for Mac/Unix users with minor modifications. It assumes you have installed Java Runtime JRE 1.7 and added it to the Window system path.

We will set up JSDoc 3 as an external tool to PhpStorm in such a way that it will work for any project by using PhpStorm macro variables and the configuration file conf.json for JSDoc 3. Furthermore, we use a template called DocStrap instead of the default template.

Step by Step Instructions

1. Start PhpStorm and clone JSDoc 3 from GitHub.
If you put it somewhere under C:\Users\Public you don’t have to worry about permissions. I put it to C:\Users\Public\Website\Tools\jsdoc3

2. [optional]. If you want to use the default template you can skip this step, otherwise clone DocStrap from GitHub into the templates directory of JSDoc 3, e.g. C:\Users\Public\Website\Tools\jsdoc3\templates

3. Open the project containing the JavaScript files you want to document and create a file named conf.json which is going to be the configuration file for JSDoc 3. Then add the following json object to it:

{
    "tags": {
        "allowUnknownTags": true
    },
    "source": {
        "include": ["./"],
        "includePattern": ".+\.js(doc)?$",
        "excludePattern": "docs"
    },
    "plugins": [],
    "templates": {
        "cleverLinks": false,
        "monospaceLinks": true,
        "systemName": "remoteFileExplorer",
        "footer": "Created by Simon Speich",
        "copyright": "2013",
        "navType": "vertical",
        "theme": "spacelab",
        "linenums": true,
        "collapseSymbols": true,
        "inverseNav": false
    },
    "opts": {
        "template": "C:\\Users\\Public\\WebsiteTools\\jsdoc3\\templates\\docstrap\\template",
        "encoding": "utf8",
        "destination": "docs/js",
        "recurse": true,
        "private": true
    }
}

include: “./” references the working directory we will set up in PhpStorm External Tools, so don’t change that.
includePattern: Only files ending in “.js” and “.jsdoc” will be processed.
excludePattern: Don’t document the documentation!
destination: “<Path to where to generate the docs>” If the path to where you want the documentation generated is outside of your project, you can also use Windows absolute paths with backslashes, but you need to escape them, e.g. C:\\Users\\Public\\Websites\\remoteFileExplorer\\docs\\js
recurse: true. Process all subfolders recursively.

Note: All quotation marks are required and all path settings are always relative to the working directory (e.g. your project) we will set up in PhpStorm below.

4. Open the Settings Window (Ctrl + Alt + S) in PhpStorm, select External Tools
and click the add button +.

Screenshot PhpStormThen provide the following information in the Edit Tools Window:

Screenshot PhpStorm

Name: JSDoc 3
This name will show up in your Tools Menu.

Group: Documentation
If you fill in a group name, then this will be shown in the Tools menu and the name JSDoc 3 will become a submenu of it.

Screenshot PhpStorm

Program: <Path to JSDoc 3jsdoc.cmd>
Fill in the path to the jsdoc.cmd, e.g.: C:\Users\Public\Website\Tools\jsdoc3\jsdoc.cmd Important on Windows: Do not use the jsdoc file directly, instead use the provided command file jsdoc.cmd, which sets up all the correct paths for you. For Mac and Unix users, the jsdoc file will probably do fine (not tested).

Parameters: -c “<Path to the conf.json file>”
Instead of hardcode the path to the configuration file we’ve created in step 2, we use the PhpStorm macro variable $FilePrompt$. This macro variable will open a file picker dialog to select the config file every time we run the JSDoc 3 Tool (did I already mention that I love PhpStorm?!).

Note: Enclose the macro variable in quotes if your path contains spaces!

Working directory: <Path to the project you want to document>
Also instead of hard wiring the path to the project you want to document such as C:\Users\Public\Websites\remoteFileExplorer, we use the macro variable $ProjectFileDir$ so JSDoc will always document the project currently open in PhpStorm.

That’s all. Happy documenting!

6 thoughts on “How to Generate JavaScript Documentation with JSDoc 3 and PhpStorm

  1. I have followed your intructions but I receive an error on publish.js file from docstrap:


    if ( members.interfaces.length ) {
    TypeError: Cannot read property 'length' of undefined

    As members.interfaces is not defined, I do not know if this error is related to docstrap release or in the conf.json file.

Leave a Reply

Your email address will not be published. Required fields are marked *