javascript - 无法读取 gulp 中未定义的属性 'watch'

标签 javascript node.js gulp

有人可以帮忙吗?我不断收到此错误,我真的不知道还能做什么。当我在 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;

最佳答案

我认为你的解构有点错误。你有:

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

我认为你想用watch替换解构中的gulp,所以现在是:

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

并在您的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;

关于javascript - 无法读取 gulp 中未定义的属性 'watch',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59085999/

相关文章:

javascript - 如何使用原型(prototype)更新粗箭头函数?

node.js - 使用 Azure CI/CD 管道中的 npm 工作区来构建 React Web 应用程序?

gulp - gulp.parallel() 或 gulp.series() 内的条件 gulp 任务

node.js - 升级gradle到6.0需要ivy.xml和pom文件

javascript - 获取包含超过 0xffff 的 unicode 字符的字符串长度

javascript - Angular Pane 拆分器,其中具有表单抛出表单验证错误的 Pane 之一

php - PHP 中的 Javascript 变量

node.js - 使用 Meteor.js 进行抓取

Node.js + Nginx - 现在怎么办?

javascript - 如何在 gulp 的嵌套目录中 bundle 多个文件?