Gulp/Node.js错误:连接ECONNREFUSED

3

我目前正在使用WAMP运行本地服务器,我正在尝试设置Gulp.js。以下是我的gulpfile的内容:

var gulp = require('gulp'),
    sass = require('gulp-ruby-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),
    minifyhtml = require('gulp-minify-html'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    clean = require('gulp-clean'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    lr = require('tiny-lr'),
    server = lr();

gulp.task('styles', function() {
    gulp.src('src/styles/main.scss')
        .pipe(sass({ style: 'expanded' }))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(gulp.dest('dist/assets/css'))
        .pipe(rename({suffix: '.min'}))
        .pipe(minifycss())
        .pipe(gulp.dest('dist/css'))
        .pipe(livereload(server))
        .pipe(notify({ message: 'Styles task complete' }));
});

gulp.task('html', function() {
    gulp.src('src/*.php')
        .pipe(minifyhtml())
        .pipe(gulp.dest('dist'))
        .pipe(livereload(server))
        .pipe(notify({ message: 'HTML task complete' }));
});

gulp.task('default', function() {
    gulp.run('html');
});

以下是运行gulp时我得到的输出结果:
[gulp] Using gulpfile C:\wamp\www\SawyerK\gulpfile.js
[gulp] Starting 'default'...
gulp.run() has been deprecated. Use task dependencies or gulp.watch task trigger
ing instead.
[gulp] Starting 'html'...
[gulp] Finished 'html' after 132 ms
[gulp] Finished 'default' after 134 ms
[gulp] gulp-notify: [Gulp notification] HTML task complete

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

根据我的发现,这个错误是由于其他应用程序正在使用Node想要使用的端口或类似情况引起的。但我不确定这是否正确,也不知道如何解决。有什么想法吗?谢谢!

编辑:如果有帮助的话,我似乎已经将其缩小到gulp-notify。

2个回答

3
我认为问题在于您在样式任务和HTML中都使用了livereload两次。也许最好使用watcher与livereload一起使用:
gulp.task('watch', function() {
  var server = livereload();
  gulp.watch('build/**').on('change', function(file) {
      server.changed(file.path);
  });
});

更新: 非常抱歉,我之前说错了 =) 这里有一篇很棒的文章,第二部分展示如何使用watch任务。 链接 要使用livereload功能,您需要使用listen方法启动服务器。


谢谢回复!但现在我有另一个问题。例如,我想使用您的手表功能,但也要使其运行适当的函数来处理文件,然后在完成后重新加载页面。这是我目前拥有的,但它不起作用:gulp.task('watch', function() { var server = livereload(); gulp.watch('src/**').on('change', function(file) { server.changed(file.path); }); gulp.watch('src/*.php', ['minifyhtml']); }); 编辑:这个评论怎么才能接受格式?我无法换行或格式化代码。 - Sawyer Knoblich
要在注释中传递代码,只需添加 符号,例如var foo = "bar";`。 - 4ega
这是它的功能,它不会保留任何格式。`gulp.task('watch', function() { var server = livereload(); gulp.watch('src/**').on('change', function(file) { server.changed(file.path); }); gulp.watch('src/*.php', ['minifyhtml']); });` - Sawyer Knoblich

0

注意:gulp-notify现在可以回退到“任务栏气球”(taskbar balloons),如果未安装Growl。 :-) - mikaelb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接