Javascript 使用gulp.watch投掷;类型错误:对象#<;对象>;没有方法';手表'&引用;
因此,我在gulpfile.js中添加了一个监视任务,如下所示:Javascript 使用gulp.watch投掷;类型错误:对象#<;对象>;没有方法';手表'&引用;,javascript,node.js,gulp,watch,Javascript,Node.js,Gulp,Watch,因此,我在gulpfile.js中添加了一个监视任务,如下所示: gulp.task('watch', function(){ gulp.watch('src/style/*.less', ['css']); }); var vfs = require('vinyl-fs'); 但是当我运行它(gulpwatch)时,我会遇到以下错误: PS C:\Projects\web\basic> gulp watch [21:28:42] Using gulpfile C:\Proj
gulp.task('watch', function(){
gulp.watch('src/style/*.less', ['css']);
});
var vfs = require('vinyl-fs');
但是当我运行它(gulpwatch
)时,我会遇到以下错误:
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)
它是返回vfs.watch(glob,opt,fn)的代码>-导致崩溃的行(第40行)vfs在本例中是一个包含乙烯基fs的变量,如下所示:
gulp.task('watch', function(){
gulp.watch('src/style/*.less', ['css']);
});
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帮助我
解决方案只是重新安装node,然后卸载并重新安装项目中的所有gulp组件。
在这之后,它运行良好,因此在安装节点或gulp组件的过程中一定出了问题。我创建了一个简单的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)和乙烯基fs(2.2.1)。乙烯基fs实际上只是根据我原来的问题尝试解决它,但没有任何区别。您可以通过键入node--version
来检查节点版本。