You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行gulp watch或gulp命令时触发AssertionError错误求助

Fixing "AssertionError: Task function must be specified" in Gulp

Hey there, that error pops up because you're using Gulp 4.x with code written for Gulp 3.x—they completely changed how task registration and dependencies work between versions. Let's break down your fix options:

Option 1: Downgrade to Gulp 3.x (Quick, No-Code Change Fix)

If you want to keep your existing gulpfile.js exactly as-is, uninstall your current Gulp version and install the latest stable 3.x release:

npm uninstall gulp --save-dev
npm install gulp@3.9.1 --save-dev

Once you run these commands, your original code will work without any tweaks.

Gulp 4.x introduced better task control with gulp.series() (runs tasks one after another) and gulp.parallel() (runs tasks simultaneously). Here's how to adjust your code to fit the new rules:

Updated gulpfile.js

var gulp = require('gulp'), 
    uglify = require('gulp-uglify');

gulp.task('scripts', function() {
  return gulp.src('js/*.js') // Added return to signal async task completion
    .pipe(uglify())
    .pipe(gulp.dest('build/js'));
});

// Styles Task
gulp.task('styles', function(done) { // Added done callback to confirm task finish
  console.log('Runs Style');
  done();
});

// Watch files
gulp.task('watch', function() {
  // Use gulp.series() to trigger the scripts task on file changes
  gulp.watch('js/*.js', gulp.series('scripts'));
});

// Default task: run scripts then styles in sequence
gulp.task('default', gulp.series('scripts', 'styles'));

Key Changes Explained:

  • Task Dependencies: Replaced the old array syntax (['scripts','styles']) with gulp.series() to define sequential task execution. Use gulp.parallel() if you want tasks to run at the same time.
  • Async Signaling: Gulp 4.x needs explicit confirmation that tasks are finished. We added a return to the scripts task (since it uses streams) and a done callback to the styles task to avoid timing bugs.
  • Watch Task: Updated the watch callback to use gulp.series('scripts') instead of just the task name array—this is required for Gulp 4.x to properly trigger the task.

内容的提问来源于stack exchange,提问作者Mohamed Samy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:26:31