簡體   English   中英

在events.js中未處理的錯誤的定位位置:141

[英]Gulp location of unhandled error in events.js:141

例如,當我使用comman gulp scripts:common運行任務時,得到以下輸出:

[14:05:47] Requiring external module babel-core/register
[14:05:49] Using gulpfile /home/sites/blablabla/gulpfile.babel.js
[14:05:49] Starting 'scripts:common'...

events.js:141
      throw er; // Unhandled 'error' event
      ^
SyntaxError: Unexpected token

好吧,對於SyntaxError,了解它在哪里發現了意外令牌將很有用。 如何告訴它至少顯示文件和行? 在哪里可以找到evetns.js文件? 我可以把console.trace()或類似的東西放在那里嗎?

我通過在腳本上運行jshint來解決此問題:

/*
 * `gulp check_scripts` - Lints script files
 */
gulp.task('check_scripts', function() {
  return gulp.src([
    'gulpfile.js' //, other scripts to check
  ])
  .pipe(jshint())
  .pipe(jshint.reporter('jshint-stylish'))
  .pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
});

enabled.failJSHint允許錯誤在本地環境中傳遞,但在生產中失敗。

這將消除腳本中的任何語法錯誤。 另外,您可能希望將其掛接到其他任務,以便在正確構建之前自動運行它:

gulp.task('default', ['check_scripts', 'clean'], function() {
  gulp.start('build');
});

這是一般的想法。

您可以通過向gulp任務添加自定義“錯誤時”處理程序來查看錯誤堆棧跟蹤。

gulp.task('compile:js', function() {
    return (
        gulp.src(jsPath)
        .pipe(yourCustomTask())
        .on('error', function(err) {
            console.log(err.stack);
            this.end();
        })
    );
});

另外,作為另一種變體,將gulp-plumber添加到管道中會使錯誤消息更加清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM