【问题标题】:Gulp "Task never defined: default"Gulp“任务从未定义:默认”
【发布时间】:2021-10-25 13:17:12
【问题描述】:

如何解决我的问题? 这是我的 gulpfile.js

const gulp = require('gulp');
const webp = require('gulp-webp');
const browserSync = require('browser-sync');

const origin = 'app';
const destination = 'app';

gulp.task('webp', () =>
gulp
.src(`${origin}/img/**`)
.pipe(webp())
.pipe(gulp.dest(`${destination}/img`))
);

gulp.task('live', function () {
gulp.watch('**/*.css').on('change', function () {
browserSync.reload();
 });
});

这是我的 package.json

{
 "name": "gulp-browsersync",
 "version": "1.0.0",
 "description": "",
 "main": "index.js",
 "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "watch": "node-sass --watch app/scss -o app/css"
 },
 "keywords": [],
 "author": "",
 "license": "ISC",
 "devDependencies": {
   "browser-sync": "^2.26.10",
   "gulp": "^4.0.2",
   "gulp-webp": "^4.0.1",
   "node-sass": "^4.14.1"
 },
 "dependencies": {}
 }

这是我的终端日志

[13:39:25] Using gulpfile ~\Desktop\gulp-browsersync-main\gulpfile.js
[13:39:25] Task never defined: default
[13:39:25] To list available tasks, try running: gulp --tasks  

【问题讨论】:

    标签: node.js npm gulp


    【解决方案1】:

    你需要从你的gulpfile.jsexports 一个函数作为default

    检查gulp-doccreating-tasks

    gulp 需要一个函数来启动任务,你必须 exports.default 你的主要任务函数。 如果您有多个任务,您需要使用seriesparallel

    代码:

    const gulp = require('gulp');
    const webp = require('gulp-webp');
    const browserSync = require('browser-sync');
    
    const origin = 'app';
    const destination = 'app';
    
    
    
    function defaultTask(cb) {
        gulp.task('webp', () =>
            gulp
                .src(`${origin}/img/**`)
                .pipe(webp())
                .pipe(gulp.dest(`${destination}/img`))
        );
    
        gulp.task('live', function () {
            gulp.watch('**/*.css').on('change', function () {
                browserSync.reload();
            });
        });
    
        cb();
    }
    
    exports.default = defaultTask
    

    【讨论】:

    • 如果我做类似的事情,我在终端 [14:00:42] 使用 gulpfile ~\Desktop\gulp-browsersync-main\gulpfile.js [14:00:42] 开始'default'... [14:00:42] 以下任务未完成:default [14:00:42] 您是否忘记发出异步完成信号?
    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 2019-09-20
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多