执行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.
Option 2: Update Code for Gulp 4.x (Recommended Long-Term)
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']) withgulp.series()to define sequential task execution. Usegulp.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
returnto thescriptstask (since it uses streams) and adonecallback to thestylestask 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
相关产品推荐
相关产品推荐

