Use JSDoc 3 and PhpStorm to generate JavaScript documentation

This is an update to my former installation guide How to Generate JavaScript Documentation with JSDoc 3 and PhpStorm, which was written in 2013. Since then, JSDoc 3 moved from Mozilla’s Rhino to Node.js to generate the documentation, which is why that guide no longer works (unless you use an older version of JSDoc 3). This installation guide is written for Windows, but should also work on Linux and Mac.

Install and integrate Node.js

  1. Download and install Node.js.
  2. Integrate Node.js into PhpStorm:
    Open Settings (Ctrl+Alt+S) -> Plugins -> Install JetBrains Plugins -> Search for Node.js -> Install the Plugin
    Install NodeJS in PhpStormOpen Settings (Ctrl+Alt+S)  again and go to Languages & Frameworks -> Node.js and NPM. In the Node interpreter field, specify the local Node.js interpreter to use. Also press the Enable Node.js core button to enable code assist. For more details about Node.js in PhpStorm read IntelliJ IDEA 2016.1 Help | Node.js
  3. Install the node package manager npm from within PhpStorm:
    Install the npm package manager by clicking on the green add button icon add button. Beware to make the installation global by setting Options to -g, otherwise you end up with npm in your project folder. The -g option will install npm into your folder %AppData%/npm/step02For more details about using the package manager in PhpStorm read Installing and Removing External Software Using Node Package Manager.

Install and integrate JSDoc

  1. Install JSDoc with the npm package manager directly from within PhpStrom:
    Open Settings (Ctrl+Alt+S) -> Languages & Frameworks -> Node.js and NPM. Click on the green add button addbutton to install a new package and search for JSDoc. Install the package. Again, use the Options checkbox and specify which folder JSDoc should be installed into or type -g to install globally, e.g. %AppData%/npm/node_modules/jsdoc/.
    Screenshot of PhpStorm
  2. Optional: Install a different template such as DocStrap. You can do this also directly from within PhpStorm the same way you just installed JSDoc above by using the npm package manager. Just search for ink-docstrap. Remember: Don’t forget the global switch!

    Note: I have to document a lot of dojo AMD modules, which return a class created with dojo/_base/declare(). I tested several templates, so far the standard JsDoc template is the only one that documents both the module and the class.

  3. Integrate JSDoc into PhpStorm by creating an external tool and custom menu:
    Open the Settings Window again (Ctrl+Alt+S), select External Tools and click the green add button icon add button.step04
    Then provide the following information in the Edit Tool Window:
    Name: JSDoc
    This name will show up in your Tools Menu.
    Group: Documentation
    If you fill in a group name, this will be shown in Tools menu and the name JSDoc will become a submenu of it.Screenshot PhpStorm
    Program: <Path to node.exe>
    Fill in the path to node.exe, e.g. C:\Program Files (x86)\tools\nodejs\node.exe
    Parameters: <Path to jsdoc.js> -c “<Path to the conf.json file>”
    Fill in the path to jsdoc.js, e.g. C:\Users\Simon\AppData\Roaming\npm\node_modules\jsdoc\jsdoc.js
    Instead of passing all the command-line arguments to JSDoc, we will use a configuration file by using the -c parameter. Also, we use the PhpStorm macro variable $FilePrompt$ instead of hard coding the path to the configuration file that we will create below. This macro variable will open a file picker dialog to select the config file every time we run the JSDoc Tool (I love PhpStorm!).
    Important: If any of your paths contain spaces, you have to enclose them in quotes. This is also true for the $FilePrompt$ macro!!!
    Working directory: <Path to the project you want to document>
    Also, instead of hard coding 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.

Configure JSDoc

The easiest way of configuring JSDoc, is to use a configuration file instead of the command-line arguments:

{
    "tags": {
        "allowUnknownTags": true,
        "dictionaries": ["jsdoc"]
    },
    "source": {
        "include": ["./"],
        "includePattern": ".+\\.js$",
        "excludePattern": "(docs)"
    },
    "plugins": [],
    "templates": {
        "cleverLinks": false,
        "monospaceLinks": true,
        "systemName": "NAFIDAS",
        "footer": "Created by Simon Speich",
        "copyright": "2013",
        "navType": "vertical",
        "theme": "spacelab",
        "linenums": true,
        "collapseSymbols": true,
        "inverseNav": false
    },
    "opts": {
        "template": "../ink-docstrap/template",
        "encoding": "utf8",
        "destination": "docs/js",
        "recurse": true,
        "private": true
    }
}

include: “./” references the current working directory, so don’t change that.
includePattern: Only files ending in “.js” 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.
template: Optional. “<path to the docstrap template>”

That’s all. Happy documenting!

One thought on “Use JSDoc 3 and PhpStorm to generate JavaScript documentation

Leave a Reply

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