Gulpfile.js:如何修复" TypeError:" listener"论证必须是一个功能"?

时间:2017-01-09 17:03:03

标签: javascript node.js gulp

我开始设置我的开发环境,当我在终端上运行gulp时遇到了一些麻烦。 我不知道这个错误来自哪里。 这是我的Gulpfile.js的代码:

var gulp        = require('gulp'),
    sass        = require('gulp-sass'),
    browserSync = require('browser-sync').create(),
    reload      = browserSync.reload,
    php         = require('gulp-connect-php'),
    source      = './sass',
    dest        = './css';

gulp.task('sass', function(){
  gulp.src(source+'/main.scss')
    .pipe(sass())
    .pipe(gulp.dest(dest))
    .on('error', onError)
    .pipe(reload({stream: true}));
});
// Watch php files
gulp.task('watch', function(){
    gulp.watch('blog/**/*.php');
    gulp.watch('admin/**/*.php');
    gulp.watch('portoflio/**/*.php');
    gulp.watch('functions/**/*.php');
    gulp.watch('templates/**/*.php');
    gulp.watch('index.php');
});

gulp.task('php', function() {
    php.server({
       base: './',
       port: 8010,
       keepalive: true
     });
});
gulp.task('browser-sync',['php'], function() {
    browserSync.init({
        proxy: 'http://localhost/username',
        files: ["**/*.php"],
        port: 8080,
        open: true,
        notify: false
    });
    gulp.watch(".sass/**/*.scss", ['sass']);
    // Watches for .php file changes
    gulp.watch("**/*.php").on("change", [reload]);
});
gulp.task('default', ['sass','browser-sync']);
function onError(err) {
  console.log(err);
  this.emit('end');
}

以下是终端中的错误:

    TypeError: "listener" argument must be a function
    at _addListener (events.js:216:11)
    at EventEmitter.addListener (events.js:276:10)
    at Gulp.<anonymous> (/Applications/MAMP/htdocs/username/gulpfile.js:43:28)
    at module.exports (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
    at /Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/index.js:279:18
    at finish (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
    at module.exports (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:60:3)
    at Gulp.Orchestrator._runTask (/Applications/MAMP/htdocs/username/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
[Mon Jan  9 17:55:57 2017] Failed to listen on 127.0.0.1:8010 (reason: Address already in use)

1 个答案:

答案 0 :(得分:0)

好像您的端口已在使用中。

[Mon Jan 9 17:55:57 2017] Failed to listen on 127.0.0.1:8010 (reason: Address already in use)

您可以检查在端口8010上运行的进程并将其关闭。

或者只是将端口更改为其他类似

的端口
gulp.task('php', function() {
    php.server({
       base: './',
       port: 8020,
       keepalive: true
     });
});

如果这样可行,那么您需要终止已经占用您端口的进程。