我有以下gulpfile.js,我通过命令行gulp消息执行:

var gulp = require('gulp');

gulp.task('message', function() {
  console.log("HTTP Server Started");
});

我得到以下错误消息:

[14:14:41] Using gulpfile ~\Documents\node\first\gulpfile.js
[14:14:41] Starting 'message'...
HTTP Server Started
[14:14:41] The following tasks did not complete: message
[14:14:41] Did you forget to signal async completion?

我在Windows 10系统上使用gulp 4。下面是gulp——version的输出:

[14:15:15] CLI version 0.4.0
[14:15:15] Local version 4.0.0-alpha.2

当前回答

我不能说我在这方面很有知识,但我也遇到过同样的问题,并且已经解决了。

解决这个问题的第七种方法是使用async函数。

编写函数时,要加上async前缀。

通过这样做,Gulp将函数包装在承诺中,任务将正常运行。

例子:

async function() {
  // do something
};

资源:

Gulp页面的最后一节:使用Async /await。 Mozilla异步函数文档。

其他回答

解决方案:我们需要调用回调函数(任务和匿名):

function electronTask(callbackA)
{
    return gulp.series(myFirstTask, mySeccondTask, (callbackB) =>
    {
        callbackA();
        callbackB();
    })();    
}

解决方案很简单,但我概述了我所做的更改、我得到的错误、前后的gulpfile以及包版本——因此使它看起来很长。

除了遵循保存.scss文件时输出的错误外,我还通过遵循前面多个答案的方向解决了这个问题。

简而言之:

我改变了gulp-sass的输入方式——见(A) 我把所有函数都改成了ASYNC函数——参见(B)

(A) gulp-sass import的改动:

之前:var sass = require('gulp-sass) After: var sass = require('gulp-sass')(require('sass'));

简单地将函数转换为ASYNC -

我的gulpfile看起来像以前:

'use strict';
 
// dependencies
var gulp = require('gulp');
var sass = require('gulp-sass');
var minifyCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var changed = require('gulp-changed');
 
var SCSS_SRC = './src/Assets/scss/**/*.scss';
var SCSS_DEST = './src/Assets/css';
 
function compile_scss() {
    return gulp.src(SCSS_SRC)
        .pipe(sass().on('error', sass.logError))
        .pipe(minifyCSS())
        .pipe(rename({ suffix: '.min' }))
        .pipe(changed(SCSS_DEST))
        .pipe(gulp.dest(SCSS_DEST));
}
 
 
function watch_scss() {
    gulp.watch(SCSS_SRC, compile_scss);
}

gulp.task('default', watch_scss); //Run tasks
 
exports.compile_scss = compile_scss;
exports.watch_scss = watch_scss;

我的gulpfile看起来像:

'use strict';
 
// dependencies
var gulp = require('gulp');
//var sass = require('gulp-sass');
var sass = require('gulp-sass')(require('sass'));
var minifyCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var changed = require('gulp-changed');

var SCSS_SRC = './src/Assets/scss/**/*.scss';
var SCSS_DEST = './src/Assets/css';
 
async function compile_scss() {
    return gulp.src(SCSS_SRC)
        .pipe(sass().on('error', sass.logError))
        .pipe(minifyCSS())
        .pipe(rename({ suffix: '.min' }))
        .pipe(changed(SCSS_DEST))
        .pipe(gulp.dest(SCSS_DEST));
}
 
async function watch_scss() {
    gulp.watch(SCSS_SRC, compile_scss);
}
 
gulp.task('default', watch_scss); // Run tasks
 
exports.compile_scss = compile_scss;
exports.watch_scss = watch_scss;

包版本:

"gulp": "^4.0.2",
"gulp-changed": "^4.0.3",
"gulp-rename": "^2.0.0",
"gulp-uglify": "^3.0.2",
"gulp-clean-css": "^4.3.0",
"gulp-sass": "^5.0.0",
"sass": "^1.38.0"

我得到的错误:

Error in plugin "gulp-sass"
Message:

gulp-sass 5 does not have a default Sass compiler; please set one yourself.
Both the `sass` and `node-sass` packages are permitted.
For example, in your gulpfile:

  var sass = require('gulp-sass')(require('sass'));

[14:00:37] The following tasks did not complete: default, compile_scss
[14:00:37] Did you forget to signal async completion?

我最近一直在努力解决这个问题,并找到了正确的方法来创建一个默认任务,运行sass然后sass:watch是:

gulp.task('default', gulp.series('sass', 'sass:watch'));

你需要做一件事:

在函数之前添加async。

const gulp = require('gulp');

gulp.task('message', async function() {
    console.log("Gulp is running...");
});

Gulp 4的一个问题。

为了解决这个问题,尝试改变您当前的代码:

gulp.task('simpleTaskName', function() {
  // code...
});

举个例子:

gulp.task('simpleTaskName', async function() {
  // code...
});

或者变成这样:

gulp.task('simpleTaskName', done => {
  // code...
  done();
});