简体   繁体   中英

How to debug a Cordova's hook?

I was wondering if is it possible to debug a javascript hook in Cordova?

My hook is triggered before prepare. My command is

cordova prepare ios

I currently use Visual Studio Code and there is a plugin "Cordova tools" to debug an app at runtime. But my need is to debug at build time.

Any recommandation?

PS : what I mean with debug is real debug, that is to say with breakpoints and display of variables, etc..

Updated answer 25 Nov 2019

Since node-inspector is deprecated, here is how I would now do this:

  • Open chrome://inspect in Chrome browser
  • Run node --inspect --inspect-brk /path/to/node_modules/cordova/bin/cordova prepare from the root of my Cordova app project which contains the hook scripts I wish to debug
  • In the Chrome tab, press inspect on the target to open Chrome Dev Tools
  • Under Filesystem tab, select Add folder to workspace and select the directory inside my Cordova project containing the hook scripts
  • Add a breakpoint to my hook script
  • Press Play in Chrome Dev Tools to proceed and hit my breakpoint

Original answer 7 Jun 2017 Here's how I debug my hook scripts:

  • Install node inspector: npm install -g node-inspector
  • From the Cordova project root directory, run the Cordova command via node inspector with appropriate options to trigger my hook script, for example:

    node-debug /path/to/node_modules/cordova/bin/cordova prepare

  • When node inspector opens in a Chrome tab, browse Sources to find your hook script

  • Add a breakpoint
  • Press Resume to continue execution to your breakpoint
  • Then you can interactively debug your hook script:

节点检查器

You can debug a Cordova hook easily inside VS Code without opening a browser by putting this launch configuration to the .vscode/launch.json file at the root of your project:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Cordova Prepare",
      "program": "C:/Program Files/nodejs/node_modules/cordova/bin/cordova", // This is for winx64 adjust it to your platform
      "args": ["prepare"]
    }
  ]
}

After just put a breakpoint in the hook's file and hit F5 or go to the Debug and Run side menu and press the Play button at the top next to the "Cordova Prepare" text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM