gulp运行报错:Task function must be specified必须指定任务函数

2019-04-0813808次阅读

运行gulp项目报错:AssertionError: Task function must be specified,那是因为你安装了gulp4.0.0,gulpfile.js用的是gulp3.9.1的语法。

package.json

gulpfile.js

AssertionError: Task function must be specified错误解决方案

  • npm i gulp@3.9.1重新安装gulp到3.91版
  • 按gulp4.0.0重写gulpfile.js任务列表

延伸:

gulp4中创建的任务是一个异步的javascript函数——一个接受错误第一次回调或返回stream, promise, event emitter, child process, 或 observable观察的函数。

任务还可以被视为公共的或私有的。

  • 公共任务是从您的gulpfile.js中exports导出的,这允许它们由gulp命令运行。
  • 私有任务被设置为在内部使用,通常用作series()或parallel()组合的一部分。
  • 私有任务看起来和其他任务一样,但最终用户无法独立执行它。要公开注册任务,请从您的gulpfile.js中exports导出该任务。
const { series } = require('gulp');

// The `clean` function is not exported so it can be considered a private task.
// It can still be used within the `series()` composition.
function clean(cb) {
  // body omitted
  cb();
}

// The `build` function is exported so it is public and can be run with the `gulp` command.
// It can also be used within the `series()` composition.
function build(cb) {
  // body omitted
  cb();
}

exports.build = build;
exports.default = series(clean, build);

从这里就可以看出对上文章标题:运行gulp项目报AssertionError: Task function must be specified错即必须指定任务函数。

不同与gulp3的还有gulp4中指定依赖任务,你需要使用gulp.series和gulp.parallel,因为gulp任务现在只有两个参数。

gulp.series按顺序执行任务

const { series } = require('gulp');

function transpile(cb) {
  // body omitted
  cb();
}

function bundle(cb) {
  // body omitted
  cb();
}

exports.build = series(transpile, bundle);

gulp.paralle最大并发性运行任务

const { parallel } = require('gulp');

function javascript(cb) {
  // body omitted
  cb();
}

function css(cb) {
  // body omitted
  cb();
}

exports.build = parallel(javascript, css);

关于更多gulp4请至https://gulpjs.com/docs/en/getting-started/creating-tasks查看

上一篇: npm设置和查看仓库源  下一篇: gulp4.0报错The following tasks did not complete  

gulp运行报错:Task function must be specified必须指定任务函数相关文章