在gulp中使用默认任务时出错,在Gitbash中显示以下错误:任务需要一个字符串名称

时间:2016-01-29 01:27:15

标签: javascript npm gulp git-bash

当我在gitbash中运行命令'gulp'时,会显示代码的最后一行的错误: 抛出新的错误('任务需要一个字符串的名称');

错误:任务需要名称为字符串

"use strict";
var gulp = require('gulp');
var connect = require('gulp-connect');  // Runs a local dev server
var open = require('gulp-open');    // Open a URL in a web browser
var config = {
port: 9005,
devBaseUrl: 'http://localhost',
paths: {
    html:  './src/*.html',
    dist: './dist'
}

};

gulp.task(connect, function(){  
connect.server({
    root: '[dist]',
    port: config.port,
    base: config.devBaseUrl,
    livereload: true
});
});

gulp.task(open,['connect'], function(){ 
gulp.src('dist/index.html')
    .pipe(open({ url:config.devBaseUrl + ':' + config.port + '/'}));
});

gulp.task(html,function(){      
gulp.src(config.paths.html)
    .pipe(gulp.src(config.paths.dist))  
    .pipe(connect.reload());
});

gulp.task('watch', function(){
gulp.watch(config.paths.html, ['html']);
});

gulp.task('default', ['html', 'open', 'watch']);    

1 个答案:

答案 0 :(得分:0)

定义任务时,第一个参数应该是字符串。

gulp.task('connect', function () {
.....
})

这是更正的文件:

var gulp = require('gulp');
var connect = require('gulp-connect');  // Runs a local dev server
var open = require('gulp-open');    // Open a URL in a web browser
var config = {
port: 9005,
devBaseUrl: 'http://localhost',
paths: {
    html:  './src/*.html',
    dist: './dist'
}

};

gulp.task('connect', function(){  
  connect.server({
    root: '[dist]',
    port: config.port,
    base: config.devBaseUrl,
    livereload: true
  });
});


gulp.task('open',['connect'], function(){ 
  gulp.src('dist/index.html')
    .pipe(open({ url:config.devBaseUrl + ':' + config.port + '/'}));
});

gulp.task('html',function(){      
  gulp.src(config.paths.html)
    .pipe(gulp.src(config.paths.dist))  
    .pipe(connect.reload());
});

gulp.task('watch', function(){
  gulp.watch(config.paths.html, ['html']);
});

gulp.task('default', ['html', 'open', 'watch']);