每次我大口吃任何东西,我都会得到一个断言错误。 -必须指定任务功能

时间:2018-06-29 09:38:08

标签: gulp gulp-watch

我正在尝试运行以下命令,但不幸的是我遇到了错误。

$ gulp build

在我的终端中,我得到这个断言错误。我已卸载节点 NPM ,然后按照以下步骤使用brew-How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)重新安装。我的节点版本是v10.5.0,npm版本是6.1.0

我的系统是 MacOS High Sierra 10.13.2

assert.js:269
    throw err;
    ^

AssertionError [ERR_ASSERTION]: Task function must be specified
    at Gulp.set [as _setTask] (bulkit/startup-kit/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (startup-kit/node_modules/undertaker/lib/task.js:13:8)
    at Object.<anonymous> 
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)

package.json

{
    "name": "bulkit-startup",
    "version": "0.0.1",
    "description": "Bulkit Startup Kit",
    "main": "Gruntfile.js",
    "devDependencies": {
        "autoprefixer": "^6.3.6",
        "browser-sync": "^2.24.5",
        "gulp": "^4.0.0",
        "gulp-clean": "^0.3.2",
        "gulp-concat": "^2.6.0",
        "gulp-postcss": "^6.1.0",
        "gulp-sass": "^2.2.0",
        "gulp-sourcemaps": "^1.6.0",
        "jquery": "^3.3.1",
        "mq4-hover-shim": "^0.3.0",
        "panini": "^1.3.0",
        "rimraf": "^2.5.2"
    },
    "engines": {
        "node": ">=0.10.1"
    },
    "scripts": {
        "start": "gulp",
        "build": "gulp build"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/cssninjaStudio/bulkit.git"
    },
    "bugs": {
        "url": "https://github.com/cssninjaStudio/bulkit/issues",
        "email": "support@cssninja.io"
    },
    "author": "Css Ninja <hello@cssninja.io> (https://cssninja.io/themes/bulkit)",
    "license": "Commercial",
    "private": true,
    "dependencies": {
        "bulma": "^0.7.0",
        "del": "^3.0.0",
        "jquery-waypoints": "^2.0.5",
        "jquery.counterup": "^2.1.0",
        "scrollreveal": "^3.4.0",
        "slick-carousel": "^1.8.1",
        "wallop": "^2.4.1"
    }
}

gulpfile.js

var gulp = require('gulp');
var clean = require('gulp-clean');
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var mq4HoverShim = require('mq4-hover-shim');
var rimraf = require('rimraf').sync;
var browser = require('browser-sync');
var panini = require('panini');
var concat = require('gulp-concat');
var port = process.env.SERVER_PORT || 8080;
var nodepath =  'node_modules/';
var assetspath =  'assets/';

// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
    browser.init({server: './_site', port: port});
});

// Watch files for changes
gulp.task('watch', function() {
    gulp.watch('scss/**/*', ['compile-scss', browser.reload]);
    gulp.watch('sass/**/*', ['compile-sass', browser.reload]);
    gulp.watch('js/**/*', ['copy-js', browser.reload]);
    gulp.watch('images/**/*', ['copy-images', browser.reload]);
    gulp.watch('html/pages/**/*', ['compile-html']);
    gulp.watch(['html/{layouts,includes,helpers,data}/**/*'], ['compile-html:reset','compile-html']);
    gulp.watch(['./src/{layouts,partials,helpers,data}/**/*'], [panini.refresh]);
});

// Erases the dist folder
gulp.task('reset', function() {
    rimraf('bulma/*');
    rimraf('scss/*');
    rimraf('assets/css/*');
    rimraf('assets/fonts/*');
    rimraf('images/*');
});

// Erases the dist folder
gulp.task('clean', function() {
    rimraf('_site');
});

// Copy Bulma filed into Bulma development folder
gulp.task('setupBulma', function() {
    //Get Bulma from node modules
    gulp.src([nodepath + 'bulma/*.sass']).pipe(gulp.dest('bulma/'));
    gulp.src([nodepath + 'bulma/**/*.sass']).pipe(gulp.dest('bulma/'));
});

// Copy static assets
gulp.task('copy', function() {
    //Copy other external font and data assets
    gulp.src(['assets/fonts/**/*']).pipe(gulp.dest('_site/assets/fonts/'));
    gulp.src([nodepath + 'slick-carousel/slick/fonts/**/*']).pipe(gulp.dest('_site/assets/css/fonts/'));
    gulp.src([nodepath + 'slick-carousel/slick/ajax-loader.gif']).pipe(gulp.dest('_site/assets/css/'));
});

//Theme Sass variables
var sassOptions = {
    errLogToConsole: true,
    outputStyle: 'compressed',
    includePaths: [nodepath + 'bulma/sass']
};

//Theme Scss variables
var scssOptions = {
    errLogToConsole: true,
    outputStyle: 'compressed',
    includePaths: ['./scss/partials']
};

// Compile Bulma Sass
gulp.task('compile-sass', function () {
    var processors = [
        mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
        autoprefixer({
            browsers: [
                "Chrome >= 45",
                "Firefox ESR",
                "Edge >= 12",
                "Explorer >= 10",
                "iOS >= 9",
                "Safari >= 9",
                "Android >= 4.4",
                "Opera >= 30"
            ]
        })//,
        //cssnano(),
    ];
    //Watch me get Sassy
    return gulp.src('./bulma/bulma.sass')
        .pipe(sourcemaps.init())
        .pipe(sass(sassOptions).on('error', sass.logError))
        .pipe(postcss(processors))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('./_site/assets/css/'));
});

// Compile Theme Scss
gulp.task('compile-scss', function () {
    var processors = [
        mq4HoverShim.postprocessorFor({ hoverSelectorPrefix: '.is-true-hover ' }),
        autoprefixer({
            browsers: [
                "Chrome >= 45",
                "Firefox ESR",
                "Edge >= 12",
                "Explorer >= 10",
                "iOS >= 9",
                "Safari >= 9",
                "Android >= 4.4",
                "Opera >= 30"
            ]
        })//,
        //cssnano(),
    ];
    //Watch me get Sassy
    return gulp.src('./scss/core.scss')
        .pipe(sourcemaps.init())
        .pipe(sass(sassOptions).on('error', sass.logError))
        .pipe(postcss(processors))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest('./_site/assets/css/'));
});

// Compile Html
gulp.task('compile-html', function() {
    gulp.src('html/pages/**/*.html')
        .pipe(panini({
        root: 'html/pages/',
        layouts: 'html/layouts/',
        partials: 'html/includes/',
        helpers: 'html/helpers/',
        data: 'html/data/'
    }))
        .pipe(gulp.dest('_site'))
        .on('finish', browser.reload);
});

gulp.task('compile-html:reset', function(done) {
    panini.refresh();
    done();
});

// Compile css from node modules
gulp.task('compile-css', function() {
    return gulp.src([ 
        nodepath + 'slick-carousel/slick/slick.css',
        nodepath + 'slick-carousel/slick/slick-theme.css',
        nodepath + 'wallop/css/wallop.css',
        //Additional static css assets
        assetspath + 'css/icons.min.css',
    ])
        .pipe(concat('app.css'))
        .pipe(gulp.dest('./_site/assets/css/'));
});

// Compile js from node modules
gulp.task('compile-js', function() {
    return gulp.src([ 
        nodepath + 'jquery/dist/jquery.min.js', 
        nodepath + 'slick-carousel/slick/slick.min.js', 
        nodepath + 'scrollreveal/dist/scrollreveal.min.js',
        nodepath + 'waypoints/lib/jquery.waypoints.min.js',
        nodepath + 'waypoints/lib/shortcuts/sticky.min.js',
        nodepath + 'jquery.counterup/jquery.counterup.min.js',
        nodepath + 'wallop/js/Wallop.min.js',
        //Additional static js assets
        assetspath + 'js/ggpopover/ggpopover.min.js',
        assetspath + 'js/ggpopover/ggtooltip.js',
        assetspath + 'js/embed/embed.js',
        assetspath + 'js/gmap/gmap.min.js',
    ])
        .pipe(concat('app.js'))
        .pipe(gulp.dest('./_site/assets/js/'));
});

//Copy Theme js to production site
gulp.task('copy-js', function() {
    gulp.src('js/**/*.js')
        .pipe(gulp.dest('./_site/assets/js/'));
});

//Copy images to production site
gulp.task('copy-images', function() {
    gulp.src('images/**/*')
        .pipe(gulp.dest('./_site/assets/images/'));
});

gulp.task('init', ['setupBulma']);
gulp.task('build', ['clean','copy', 'compile-js', 'compile-css', 'copy-js', 'compile-sass', 'compile-scss', 'compile-html', 'copy-images']);
gulp.task('default', ['server', 'watch']);

12 个答案:

答案 0 :(得分:55)

Gulp 4.0更改了如果任务依赖于另一个要执行的任务,则应定义任务的方式。 list参数已被弃用。

您的gulpfile.js示例如下:

// Starts a BrowerSync instance
gulp.task('server', ['build'], function(){
  browser.init({server: './_site', port: port});
});

他们引入了gulp.series()gulp.parallel()而不是list参数。

此任务应更改为以下内容:

// Starts a BrowerSync instance
gulp.task('server', gulp.series('build', function(){
  browser.init({server: './_site', port: port});
}));

我不是这方面的专家。您可以在gulp documentation for running tasks in series或以下Jhey Thompkins和Stefan Baumgartner的出色博客文章中看到更可靠的示例

https://codeburst.io/switching-to-gulp-4-0-271ae63530c0

https://fettblog.eu/gulp-4-parallel-and-series/

答案 1 :(得分:14)

将package.json文件中的gulp版本降低到3.9.1-

"gulp": "^3.9.1",

答案 2 :(得分:6)

尝试替换您的gulpfile.js的最后一行

gulp.task('default', ['server', 'watch']);

使用

gulp.task('default', gulp.series('server', 'watch'));

答案 3 :(得分:4)

https://fettblog.eu/gulp-4-parallel-and-series/

因为 gulp.task(name, deps, func)替换为gulp.task(name, gulp.{series|parallel}(deps, func))

您使用的是最新版本的gulp,但代码较旧。修改代码或降级。

答案 4 :(得分:2)

您无需将gulp从 gulp 4 降级。使用gulp.series()组合多个任务。首先,使用

全局安装gulp

MongoPool.js // This creates a pool with default size of 5 // This gives client; You can add few lines to get db if you wish // connection is a promise let connection; module.exports.getConnection = () => { connection = MongoClient(url).connect() } module.exports.getClient = () => connection Now in your test you could, const { getConnection } = require('./MongoPool') ... describe('Forecast', () => { // get client connection getConnection() ... In your route: ... const { getClient } = require('./MongoPool') router.get('/forecast', (req, res) => { // if you made sure you called getConnection() elsewhere in your code, client is a promise (which resolves to mongodb connection pool) const client = getClient() // do something with DB here // then you could do something like client.db('db-name').then(//more).catch() })

然后使用

在您的工作目录上本地安装

npm install --global gulp-cli

see details here

示例:

package.json

npm install --save-dev gulp

gulpfile.js

{
  "name": "gulp-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "browser-sync": "^2.26.3",
    "gulp": "^4.0.0",
    "gulp-sass": "^4.0.2"
  },
  "dependencies": {
    "bootstrap": "^4.3.1",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.7"
  }
}

运行var gulp = require("gulp"); var sass = require('gulp-sass'); var browserSync = require('browser-sync').create(); // Specific Task function js() { return gulp .src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 'node_modules/jquery/dist/jquery.min.js', 'node_modules/popper.js/dist/umd/popper.min.js']) .pipe(gulp.dest('src/js')) .pipe(browserSync.stream()); } gulp.task(js); // Specific Task function gulpSass() { return gulp .src(['src/scss/*.scss']) .pipe(sass()) .pipe(gulp.dest('src/css')) .pipe(browserSync.stream()); } gulp.task(gulpSass); // Run multiple tasks gulp.task('start', gulp.series(js, gulpSass)); 可以启动多个任务,并运行gulp startgulp js来执行特定任务。

答案 5 :(得分:1)

使用Gulp时出现相同的错误。解决方案是将本地版本和CLI版本都切换到Gulp 3.9.1版。

sudo npm install -g gulp@3.9.1

在项目文件夹中运行

npm install gulp@3.9.1

答案 6 :(得分:1)

为了修正错误而不断更改gulp和npm版本不是很好。重新安装工作机后的最后几天,我遇到了几个例外。浪费大量的时间来重新安装和修复这些问题。

因此,我决定将所有版本升级到最新版本:

npm -v : v12.13.0 
node -v : 6.13.0
gulp -v : CLI version: 2.2.0 Local version: 4.0.2

此错误的出现是由于它在gulpfile中的编码方式,而不是版本不匹配。 因此,在这里您必须更改gulpfile中的2项内容以与Gulp版本4保持一致。 Gulp 4与版本3相比,更改了启动任务的方式。

  1. 在版本4中,您必须先将该任务定义为一个函数,然后再通过其字符串名称将该任务称为gulp任务。在V3中:
  

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

但是在V4中,它应该是这样的:

function serve() {
...
}
gulp.task('serve', gulp.series(sass));
  1. 正如@Arthur所提到的,您需要更改将参数传递给task函数的方式。在V3中就是这样的:
  

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

但是在V4中,应该是:

gulp.task('serve', gulp.series(sass));

答案 7 :(得分:1)

问题是您正在使用gulp 4,而gulfile.js中的语法是gulp3。因此,请将gulp降级为3.x.x或使用gulp 4语法。

语法Gulp 3:

gulp.task('default', ['sass'], function() {....} );

语法Gulp 4:

gulp.task('default', gulp.series(sass), function() {....} );

您可以在以下位置阅读有关gulp和gulp任务的更多信息: https://medium.com/@sudoanushil/how-to-write-gulp-tasks-ce1b1b7a7e81

答案 8 :(得分:1)

用 gulp.series() 替换 [] 肯定对我有用,我需要做的另一个更改是移动我的主要任务“例如”。使用 Gulp v4.02 将 gulp main-task' 添加到 Gulp 文件的末尾

答案 9 :(得分:0)

从 Gulp 3.x 迁移到 Gulp 4.x 时,我们会遇到以下常见错误:

错误:

  • AssertionError [ERR_ASSERTION]:必须指定任务函数
  • 断言错误 [ERR_ASSERTION]:任务从未定义:
  • 以下任务未完成:您是否忘记发出异步完成信号?

在任务定义和执行方面需要注意以下几点:

  1. 首先定义任务
  2. 使用返回
  3. 然后用 gulp.series() 执行

这是与 Gulp v4 一起使用的快速 Gulp 代码

// First define task with Return
gulp.task('css-minify', function () {
    return gulp.src('assets/images/*.*')
        .pipe(smushit({verbose: true}))
        .pipe(gulp.dest('assets/images'));
});

// Then execute task with bracket '()' instead of '[]'
gulp.task('default', gulp.series('css-minify'));

答案 10 :(得分:0)

使用 gulp.task( gulp.series() ) 更新 gulp.task() 后工作;

答案 11 :(得分:-2)

步骤:

  1. “ gulp”:“ ^ 3.9.1”,
  2. npm安装
  3. gulp样式