当我尝试在cmment行上运行gulp时,gulp 4.0.0显示错误

时间:2019-01-14 02:52:18

标签: node.js gulp

我正在尝试制作一个新项目, 我在这里使用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)

1 个答案:

答案 0 :(得分:1)

这在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 documentation: creating tasks