简体   繁体   English

无法读取 gulp 中未定义的属性“手表”

[英]Cannot read property 'watch' of undefined in gulp

Can some one help please?有人可以帮忙吗? I keep recieving this error and I really dont know what else to do.我一直收到这个错误,我真的不知道还能做什么。 When I trigger the command gulp watch in the cmd, I receive the following error TypeError: Cannot read property 'watch' of undefined当我在 cmd 中触发命令gulp watch时,我收到以下错误TypeError: Cannot read property 'watch' of undefined

 C:\\Apache24\\htdocs\\Gulp>gulp watch [10:37:07] Using gulpfile C:\\Apache24\\htdocs\\Gulp\\gulpfile.js [10:37:07] Starting 'watch'... [10:37:07] 'watch' errored after 4.16 ms [10:37:07] TypeError: Cannot read property 'watch' of undefined at watch (C:\\Apache24\\htdocs\\Gulp\\gulpfile.js:42:10) at watch (C:\\Apache24\\htdocs\\Gulp\\node_modules\\undertaker\\lib\\set-task.js:13:15) at bound (domain.js:422:14) at runBound (domain.js:435:12) at asyncRunner (C:\\Apache24\\htdocs\\Gulp\\node_modules\\async-done\\index.js:55:18) at processTicksAndRejections (internal/process/task_queues.js:75:11)
const {src, dest, series, gulp, parallel} = require('gulp');
var rename = require('gulp-rename');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var sourcemaps = require('gulp-sourcemaps');

var styleSRC = './src/scss/style.scss';
var styleDIST = './dist/css/';


var jsSRC = './src/js/script.js';
var jsDIST = './dist/js/';


function style() {
    "use strict";
    return src(styleSRC)
        .pipe(sourcemaps.init())
        .pipe(sass({
            errorLogToConsole: true,
            outputStyle: 'compressed'
        }))
        .on('error', console.error.bind(console))
        .pipe(autoprefixer({
            overrideBrowserslist: ["defaults"],
            cascade: false
        }))
        .pipe(rename({basename: 'style', suffix: '.min'}))
        .pipe(sourcemaps.write('./'))
        .pipe(dest(styleDIST));
}


function js() {
    "use strict";
    return src(jsSRC)
        .pipe(dest(jsDIST));
}

const watch = function () {
    "use strict";
    gulp.watch('./src/scss/**/*.scss', {usePolling: true}, gulp.series(style));
    gulp.watch('./src/js/**/*.js', {usePolling: true}, gulp.series(js));
};
exports.default = series(
    parallel(style, js),
    watch
);

exports.watch = watch;
exports.js = js;
exports.style = style;

Try this, remove const and define it as a function.试试这个,删除 const 并将其定义为函数。

function watch() {
    "use strict";
    gulp.watch('./src/scss/**/*.scss', {usePolling: true}, gulp.series(style));
    gulp.watch('./src/js/**/*.js', {usePolling: true}, gulp.series(js));
};

I think you have got your destructuring a bit wrong.我认为你的解构有点错误。 You have:你有:

const {src, dest, series, gulp, parallel} = require('gulp');
//                          ^ this does not exist - hence the undefined property

I think you want to replace the gulp in the destructuring with watch , so it's now:我想你想用watch替换解构中的gulp ,所以现在是:

const {src, dest, series, watch, parallel} = require('gulp');

and in your watch function do:并在您的watch功能中执行以下操作:

// renamed the function to avoid conflict
const watchTask = function () {
    "use strict";
    watch('./src/scss/**/*.scss', {usePolling: true}, series(style));
    watch('./src/js/**/*.js', {usePolling: true}, series(js));
};

exports.default = series(
    parallel(style, js),
    watchTask
);

exports.watch = watchTask;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM