如何debuggingGulp任务?

如何使用debugging器(如Google Chromedebugging器)debugginggulpfile.js定义的gulpfile.js任务, gulpfile.js执行任务的代码?

通过使用--inspect标志, Node.js 6.3+版本的debugging变得更简单了。

debugging一个名为build的gulp任务:

  1. 找出你的gulp可执行程序的位置。 如果node_modules/.bin/gulp在本地安装,则会在node_modules/.bin/gulp 。 否则,在terminal中,运行Linux / Mac上的which gulpwhere gulp ,或者在Windows上进行search以find它。

  2. 运行node --inspect --debug-brk ./node_modules/.bin/gulp build 。 如果需要,请将./node_modules/.bin/gulpreplace为您的./node_modules/.bin/gulp安装path。

  3. 使用Chrome浏览到打印到控制台的URL。 它看起来像chrome-devtools://devtools/remote/serve_file/@xxxxxxxxx/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/xxx-xxx-xxx

--debug-brk标志用于立即暂停代码执行。 这使您有机会打开浏览器并在任务完成之前设置断点。

您还可以安装Chrome的Node.js检查器pipe理器(NIM)扩展程序,以帮助执行步骤3.这将自动打开一个Chrome选项卡,并且可以随时使用debugging程序,以替代从terminal复制和粘贴URL。

如果您使用的是webstorm,您可以右键单击吞食面板中的任务并selectdebugging。

在这里输入图像描述

如果你使用的是gulp-nodemon,你可以在你的gulp文件中做到这一点。 只要将它传递给execMap选项:

 gulp.task('default', function() { nodemon({ script: 'server.js', ext: 'js', execMap: { js: "node --inspect" } }) } 

希望这可以帮助。