首页 > 解决方案 > 当我尝试在 cmment 线上运行 gulp 时,gulp 4.0.0 显示错误

问题描述

我正在尝试制作一个新项目,我在这里使用gulpfile.ja代码

const gulp = require('gulp');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass');

// Compile SASS
gulp.task('sass', function(){
  return gulp.src(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'])
    .pipe(sass())
    .pipe(gulp.dest("src/css"))
    .pipe(browserSync.stream());
});

// Move JS Files to SRC
gulp.task('js', function(){
  return gulp.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/tether/dist/js/tether.min.js'])
    .pipe(gulp.dest("src/js"))
    .pipe(browserSync.stream());
});

// Watch SASS & Serve
gulp.task('serve', ['sass'], function(){
  browserSync.init({
    server: "./src"
  });

  gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], ['sass']);
  gulp.watch("src/*.html").on('change', browserSync.reload);
});

// Move Font Awesome Fonts folder to src
gulp.task('fonts', function(){
  return gulp.src('node_modules/font-awesome/fonts/*')
    .pipe(gulp.dest("src/fonts"));
});

// Move font awesome css file
gulp.task('fa', function(){
  return gulp.src('node_modules/font-awesome/css/font-awesome.min.css')
    .pipe(gulp.dest("src/css"));
});

gulp.task('default', ['js', 'serve', 'fa', 'fonts']);

但是当我gulp在我的命令行上运行时,这里显示一个错误,例如

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

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (D:\PracticeJob\bs4practice\node_modules\undertaker\lib\set-task.js:10:3)
    at Gulp.task (D:\PracticeJob\bs4practice\node_modules\undertaker\lib\task.js:13:8)
    at Object.<anonymous> (D:\PracticeJob\bs4practice\gulpfile.js:21:6)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)

标签: node.jsgulp

解决方案


这在 gulp4 中是不允许的:

gulp.task('default', ['js', 'serve', 'fa', 'fonts']);

改成:

gulp.task('default', gulp.series('js', 'serve', 'fa', 'fonts');

与此相同:

gulp.task('serve', ['sass'], function(){

改成:

gulp.task('serve', gulp.series('sass', function(){

并更改为:

  gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 'src/scss/*.scss'], 'sass');

您不能只在 gulp4 环境中使用 gulp3 代码而不进行这些更改。以上是您必须进行的最小更改,但您还需要进行更多更改才能充分利用 gulp4。参见例如gulp4 文档:创建任务


推荐阅读