【问题标题】:Using gulp.watch throws "TypeError: Object #<Object> has no method 'watch'"使用 gulp.watch 会抛出“TypeError: Object #<Object> has no method 'watch'”
【发布时间】:2016-02-08 19:55:00
【问题描述】:

所以我在 gulpfile.js 中添加了一个监视任务,如下所示:

gulp.task('watch', function(){

    gulp.watch('src/style/*.less', ['css']);
});

但是当我运行它时 (gulp watch) 我收到以下错误:

PS C:\Projects\web\basic> gulp watch
[21:28:42] Using gulpfile C:\Projects\web\basic\gulpfile.js
[21:28:42] Starting 'watch'...
[21:28:42] 'watch' errored after 226 μs
[21:28:42] TypeError: Object #<Object> has no method 'watch'
    at Gulp.watch (C:\Projects\web\basic\node_modules\gulp\index.js:40:14)
    at Gulp.<anonymous> (C:\Projects\web\basic\gulpfile.js:37:7)
    at module.exports (C:\Projects\web\basic\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\Projects\web\basic\node_modules\gulp\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\Projects\web\basic\node_modules\gulp\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\Projects\web\basic\node_modules\gulp\node_modules\orchestrator\index.js:134:8)
    at C:\Users\Magnus\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:20
    at process._tickCallback (node.js:415:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)

据我所知,watch 是一个 built-in function in gulp 那么我在这里做错了什么?我见过的解决这个困境的唯一其他解决方案是特定于 Linux 的(并且推测问题的原因也是如此)。它在this SO-thread 中有描述,但我使用的是 Windows,我看不出这个问题在 Windows 环境中是如何出现的。

另外,我查看了用于项目安装的 Gulp index.js-文件,它确实包含一个手表原型,但问题似乎(根据错误消息)来自watch 函数的最后一行:

Gulp.prototype.watch = function(glob, opt, fn) {
  if (typeof opt === 'function' || Array.isArray(opt)) {
    fn = opt;
    opt = null;
  }

  // Array of tasks given
  if (Array.isArray(fn)) {
    return vfs.watch(glob, opt, function() {
      this.start.apply(this, fn);
    }.bind(this));
  }

  return vfs.watch(glob, opt, fn);
};

导致崩溃的是return vfs.watch(glob, opt, fn);-line(它是第40行)。 vfs 在这种情况下是一个包​​含 vinyl-fs 的变量,如下所示:

var vfs = require('vinyl-fs');

但即使在我的项目和全局手动安装它之后,它仍然无法正常工作。任何人都知道可能导致此崩溃的原因吗?

编辑:完整的 gulpfile.js:

var gulp = require('gulp'),
    less = require('gulp-less'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify');

var cssPath = 'src/style/*.less';
var jsPath = 'src/js/*.js';
var htmlPath = 'src/index.htm';
var assetPath = 'src/assets/*';

gulp.task('css', function() {
    return gulp.src(cssPath)    //Get all less files from src/style/
        .pipe(less())                   //Pass them on to less          
        .pipe(concat('style.css'))      //Concat all files to one big style.css-file
        .pipe(gulp.dest('dist/style'))  //Pass the less result to gulp so it can be moved to dist/css
});

gulp.task('js', function(){
    return gulp.src(jsPath) //Get all the javascript files from src/js/
        .pipe(uglify())             //Pass them on to uglify
        .pipe(concat('all.js'))     //Concat all the files into one javascript file
        .pipe(gulp.dest('dist/js')) //Pass the result to gulp so it can be moved to dist/js
});

gulp.task('html', function(){
    return gulp.src(htmlPath)
        .pipe(gulp.dest('dist'))
});

gulp.task('assets', function(){
    return gulp.src(assetPath)
        .pipe(gulp.dest('dist/assets'))
});

gulp.task('watch', function(){

    gulp.watch(cssPath, ['css']);
    gulp.watch(jsPath, ['js']);
    gulp.watch(htmlPath, ['html']);
    gulp.watch(assetPath, ['assets']);
});

gulp.task('default', ['css', 'js', 'html', 'assets']);

【问题讨论】:

  • 我创建了一个简单的 gulpfile,其中包含您提供的代码和额外的 gulp 任务 css,它可以正常工作。请发布整个 gulpfile,因为问题的根源可能在其他地方。
  • 我添加了整个 gulpfile.js。我还尝试创建一个非常基本的 gulpfile,它只移动一个文件并添加一个手表,但我仍然得到同样的错误。开始感觉我的节点安装已经损坏或其他什么...
  • 您粘贴的代码可以正常工作。您使用的是什么版本的节点?另请发布您的项目中使用的已安装软件包的列表。您可以通过键入npm list --depth=0(本地)和npm list --depth=0 -g(全球)来获取它们。您是否尝试过重新安装 gulp 包?
  • Npm 版本为 1.3.2。项目包有:gulp (3.9.0)、gulp-concat(2.6.0)、gulp-less(3.0.3)、gulp-uglify(1.4.2) 和vinyl-fs(2.2.1)。乙烯基-fs实际上只是尝试根据我最初的问题尝试解决它,但它没有任何区别。
  • 您可以通过输入node --version查看节点版本。

标签: javascript node.js gulp watch


【解决方案1】:

感谢@ezrepotein 和@entre 帮助我。

解决方案是简单地重新安装节点,然后卸载并重新安装项目中的所有 gulp 组件。 之后它运行良好,所以在安装任何一个节点或 gulp 组件的过程中一定出了问题。

【讨论】:

    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多