Skip to content

How to debug Yeoman generators

Greg Swindle edited this page Apr 22, 2017 · 1 revision

ℹ️ This how-to article is for engineers who need to debug the generator-android-lib on their Mac workstations.

Table of contents


1. Install node-inspector.

Open a Terminal and run:

$ npm i -g node-inspector

2. Find the path to Yeoman's cli.js.

ℹ️ For nvm users

If you're using nvm, then you know that you might have Yeoman installed in multiple locations by Node.js version. Therefore it's important to keep aware of the Node.js version you're debugging on.

2.1. Output the yo binary path.

$ which yo

This should output something like

/Users/<user>/.nvm/versions/node/<node-version>/bin/yo

E.g.,

/Users/swindle/.nvm/versions/node/v7.7.3/bin/yo

2.2. Find the Yeoman cli.js.

$ ls /Users/<user>/.nvm/versions/node/<node-version>/lib/node_modules/yo/lib

E.g.,

$ ls -go /Users/swindle/.nvm/versions/node/v7.7.3/lib/node_modules/yo/lib

You should see cli.js in the output.

✏️ Take note...

  • Remember the path to Yeoman's cli.js.
  • We'll refer to Yeoman's cli.js path as $YO_CLI.

2.3. export an env variable to the Yeoman cli.js.

$ YO_CLI=/<path>/<to>/yo/lib/cli.js

3. Starting inspection/debugging

3.1. Run node.js with the Yeoman-cli in inspect mode.

Run the following command, where android-lib is the Yeoman generator you want to debug:

$ node --inspect $YO_CLI android-lib

This should output something like:

Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/575645d0-012a-43af-9421-ca6e5b6d10f1

     _-----_     ╭──────────────────────────╮
    |       |    │  Welcome to the Verizon  │
    |--(o)--|    │      Android Library     │
   `---------´   │        generator!
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |     
   __'.___.'__   
 ´   `  |° ´ Y `

? What are you calling your library? (jsdoc-2-md-testbed)

🔴 Did you get an "Address already in use" error?

If you get an "Address already in use" error, here's how you can kill the existing process:

  1. Run $ lsof -i @localhost:9229
  2. Get the PID from the output.
  3. Kill the process: $ kill -9 <PID>

Now you can retry node --inspect $YO_CLI android-lib.

3.2. Open chrome-devtools:

3.2.1. Open Google Chrome.

Launch Chrome or open a new tab if it's already running.

3.2.2. Open the node-inspector debug session in Chrome.

In step 3.1., you should have seen output similar to this:

To start debugging, open the following URL in Chrome:
    chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/575645d0-012a-43af-9421-ca6e5b6d10f1

Copy the entire chrome-devtools: string and paste it into a new Google Chrome tab.

3.2.3. Enable "Pause on Uncaught Exceptions"

3.2.3.1. Open the "Sources" view of chrome-devtools

[chrome-devtools-sources]

3.2.3.2. Enable "Pause on Uncaught Exceptions".

[chrome-devtools-exceptions]

4. Start debugging!

Go back to your Terminal and walk through your generator. If exceptions are thrown, you'll see them in chrome-devtools.