错误:使用gulp版本4时gulp.start不是函数

时间:2020-06-03 10:41:30

标签: gulp

使用gulp的Iam CLI版本:2.2.1本地版本:4.0.2 节点版本为12.16.3 我的gulpfile.js代码是

 'use strict';
var gulp = require('gulp'),
sass = require('gulp-sass'),
browserSync = require('browser-sync'),
del=require('del'),
imagemin = require('gulp-imagemin'),
uglify = require('gulp-uglify'),
usemin = require('gulp-usemin'),
rev = require('gulp-rev'),
cleanCss = require('gulp-clean-css'),
flatmap = require('gulp-flatmap'),
htmlmin = require('gulp-htmlmin');

gulp.task('sass', function () {
    return gulp.src('./css/*.scss')
    .pipe(sass().on('error', sass.logError))
   .pipe(gulp.dest('./css'));
});

gulp.task('sass:watch', function () {
    gulp.watch('./css/*.scss', ['sass']);
});

gulp.task('browser-sync', function () {
 var files = [
  './*.html',
  './css/*.css',
  './img/*.{png,jpg,gif}',
  './js/*.js'
];

 browserSync.init(files, {
  server: {
     baseDir: "./"
  }
 });

});

 // Default task
gulp.task('default', gulp.series('browser-sync', function() {
  gulp.start('sass:watch');
}));
 gulp.task('clean', function() {
   return del(['dist']);
});

 gulp.task('copyfonts', function() {
   gulp.src('./node_modules/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
  .pipe(gulp.dest('./dist/fonts'));
 });
 gulp.task('imagemin', function() {
  return gulp.src('img/*.{png,jpg,gif}')
  .pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
  .pipe(gulp.dest('dist/img'));
 });
  gulp.task('usemin', function() {
    return gulp.src('./*.html')
   .pipe(flatmap(function(stream, file){
    return stream
     .pipe(usemin({
          css: [ rev() ],
          html: [ function() { return htmlmin({ collapseWhitespace: true })} ],
          js: [ uglify(), rev() ],
          inlinejs: [ uglify() ],
          inlinecss: [ cleanCss(), 'concat' ]
      }))
   }))
  .pipe(gulp.dest('dist/'));
 });

 gulp.task('build',gulp.series('clean', function() {
    gulp.start('copyfonts','imagemin','usemin');
 }));

在命令行上运行 gulp构建后出现的错误是:

  [15:38:33] Starting 'build'...
  [15:38:33] Starting 'clean'...
  [15:38:33] Finished 'clean' after 69 ms
  [15:38:33] Starting '<anonymous>'...
  [15:38:33] '<anonymous>' errored after 3.57 ms
   [15:38:33] TypeError: gulp.start is not a function
  at C:\Users\HARIKA\Desktop\bootstrapassign1\Bootstrap4\conFusion\gulpfile.js
:74:10
[15:38:33] 'build' errored after 83 m

我不知道如何解决错误。我甚至根据gulp版本4的版本将一些任务更改为gulp.series()。有人可以帮助我解决该错误吗?预先谢谢你。

2 个答案:

答案 0 :(得分:3)

替换这两个任务:

 // Default task
gulp.task('default', gulp.series('browser-sync', function() {
  gulp.start('sass:watch');
}));

 gulp.task('build',gulp.series('clean', function() {
    gulp.start('copyfonts','imagemin','usemin');
 }));

具有:

 // Default task
gulp.task('default', gulp.series('browser-sync', 'sass:watch'));


gulp.task('build',gulp.series('clean', 'copyfonts','imagemin','usemin'));

gulp.start不是gulp4 +的一部分-它在v3中,尽管即使在该版本中也未得到真正的批准。

答案 1 :(得分:0)

我遇到了同样的问题,也许找到了解决方案:

替换以下内容:

gulp.task('sass:watch', function () {
  gulp.watch('./css/*.scss', ['sass']);

// Default task
gulp.task('default', gulp.series('browser-sync', function() {
  gulp.start('sass:watch');
}));

并添加:

gulp.task('sass:watch', function() {
  gulp.watch('./css/*.scss', gulp.series(['sass']));

// Default task
gulp.task('default', gulp.parallel('browser-sync', 'sass:watch'));

根据我的发现,您必须将 gulp.seriesgulp.parallel 与 gulp +4.x 互换使用

现在进行第二部分更改:

gulp.task('copyfonts', function() {
  gulp.src('./node_modules/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
  .pipe(gulp.dest('./dist/fonts'));
});

在此处添加 return

gulp.task('copyfonts', function() {
  return gulp.src('./node_modules/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
  .pipe(gulp.dest('./dist/fonts'));
});

就像上一个答案中显示的那样做最后一个任务:

gulp.task('build', gulp.series('clean','copyfonts','imagemin','usemin'));

希望这会有所帮助!