Gulp AssertionError [ERR_ASSERTION]:必须在更新Node和NPM之后指定任务功能

时间:2018-10-04 11:45:57

标签: javascript gulp gulp-watch gulp-uglify gulp-sourcemaps

我刚刚开始学习gulp,我遵循了本教程系列:https://www.youtube.com/watch?v=ax0ykSVPufs&list=PLriKzYyLb28lp0z-OMB5EYh0OHaKe91RV&index=7

在更新节点和npm版本之前,我一切正常,遇到错误。这是我的gulpfile.js

const gulp = require('gulp');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const uglify = require('gulp-uglify');
const autoprefixer = require('gulp-autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');

let styleSource = 'src/scss/style.scss';
let styleDestination = './build/css/';
let styleWatch = 'src/scss/**/*.scss';     
let jsSource = 'main.js';
let jsFolder = 'src/js/';
let jsDestination = './build/js/';
let jsWatch = 'src/js/**/*.js'; 
let jsFILES = [jsSource];
let htmlWatch = '**/*.html';


/* Converting Sass to CSS */
gulp.task('styles',function(){
    gulp.src(styleSource)
        .pipe(sourcemaps.init())
        .pipe(sass({
            errorLogToConsole: true,
            outputStyle: 'compressed'
        }))
        .on('error', console.error.bind(console))
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(rename({suffix:'.min'}))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(styleDestination));
});

/* Converting ES6 to Vanilla JS */
gulp.task('js',function(){
    jsFILES.map(function(entry){
        return browserify({
            entries: [`${jsFolder}${entry}`]
        })
        .transform(babelify, {presets:['env']})              
        .bundle()
        .pipe(source(entry))
        .pipe( rename({extname:'.min.js'}) )
        .pipe(buffer())
        .pipe(sourcemaps.init({loadMaps: true})) 
        .pipe(uglify())
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(jsDestination))
    });

})

// default task to run all tasks
gulp.task('default', ['styles','js']);

// watch default
gulp.task('watch', ['default'], function(){ 
    gulp.watch(styleWatch, ['styles']); 
    gulp.watch(jsWatch, ['js']); 
    gulp.watch(htmlWatch);
});

在终端中输入gulp命令后,出现此错误:

assert.js:42
  throw new errors.AssertionError({
  ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (D:\Web Design and Development\Tutorials\Web Design and Development\My Javascript Skills Projects\javascript30\javascript30-day-two\node_modules\undertaker\lib\set-task.js:10:3)
    at Gulp.task (D:\Web Design and Development\Tutorials\Web Design and Development\My Javascript Skills Projects\javascript30\javascript30-day-two\node_modules\undertaker\lib\task.js:13:8)
    at Object.<anonymous> (D:\Web Design and Development\Tutorials\Web Design and Development\My Javascript Skills Projects\javascript30\javascript30-day-two\gulpfile.js:63:6)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)

1 个答案:

答案 0 :(得分:2)

欢迎来到Gulp。大吃一喝4!

您的教程可能适用于Gulp 3

gulp.task('default', ['styles','js']);

由于使用了新的任务执行系统,因此在Gulp 4中无法使用

相反,您必须做类似的事情

gulp.task('default', gulp.parallel('styles','js'))

gulp.task('watch', ['default'], function(){...

更多详细信息:

https://www.liquidlight.co.uk/blog/how-do-i-update-to-gulp-4/

https://fettblog.eu/gulp-4-parallel-and-series/