使用gulp.watch抛出“TypeError:Object#<Object>没有方法'看'”

所以我添加了一个watch任务给我的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) 

正如我已经了解, 手表是一个内置的function ,所以我在这里做错了什么? 我见过的唯一的其他解决scheme就是Linux特有的(可以假设问题的原因)。 这是在这个SO线程中描述,但我使用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); 导致崩溃的行(这是第40行)。 在这种情况下, vfs是一个包含vinyl-fs的variables,如下所示:

 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']); 

感谢@ezrepotein和@entre帮助我。

解决scheme只是重新安装节点,然后卸载并重新安装项目中的所有gulp组件。 之后,它的工作得很好,所以在安装节点或者gulp组件的时候,一定会出现问题。