【问题标题】:Error: listen EADDRINUSE 127.0.0.1:3000错误:听 EADDRINUSE 127.0.0.1:3000
【发布时间】:2016-09-16 22:55:12
【问题描述】:

我使用以下示例设置快速服务器:https://github.com/sogko/gulp-recipes/tree/master/browser-sync-nodemon-expressjs

我使用 gulp 启动了几个流程,例如缩小、硫化和折叠我的 Polymer 项目。

gulpfile.js 有 364 行大,但这是最重要的部分:

// Build and serve the output from the dist build
appsPaths.forEach(function(app) {
  gulp.task('serve:dist:' + app,
    ['browser-sync', 'default:' + app]);
});

gulp.task('browser-sync', ['nodemon'], function () {

  // for more browser-sync config options: http://www.browsersync.io/docs/options/
  browserSync({
    notify: false,
    snippetOptions: {
      rule: {
        match: '<span id="browser-sync-binding"></span>',
        fn: function (snippet) {
          return snippet;
        }
      }
    },

    // informs browser-sync to proxy our expressjs app which would run at the following location
    proxy: 'http://localhost:3000',

    // informs browser-sync to use the following port for the proxied app
    // notice that the default port is 3000, which would clash with our expressjs
    port: 4000,

    // open the proxied app in chrome
    browser: ['google-chrome']
  });
});

gulp.task('nodemon', function (cb) {
  var called = false;
  return $.nodemon({

    // nodemon our expressjs server
    script: 'server/app.js',

    // watch core server file(s) that require server restart on change
    watch: ['server/app.js']
  })
    .on('start', function onStart() {
      // ensure start only got called once
      if (!called) { cb(); }
      called = true;
    })
    .on('restart', function onRestart() {
      // reload connected browsers after a slight delay
      setTimeout(function reload() {
        browserSync.reload({
          stream: false
        });
      }, BROWSER_SYNC_RELOAD_DELAY);
    });
});

现在虽然这很好用,但我仍然在我的 gulp 日志中收到以下错误:

^CDaniels-iMac:polymer dani$ gulp serve:dist:domain.com
[22:33:10] Using gulpfile ~/dev/company/polymer/gulpfile.js
[22:33:10] Starting 'nodemon'...
[22:33:10] Starting 'clean:domain.com'...
[22:33:10] Finished 'clean:domain.com' after 8.56 ms
[22:33:10] Starting 'default:domain.com'...
[22:33:10] Starting 'copy:domain.com'...
[22:33:10] Starting 'styles:domain.com'...
[22:33:11] Finished 'styles:domain.com' after 295 ms
[22:33:11] [nodemon] 1.9.2
[22:33:11] [nodemon] to restart at any time, enter `rs`
[22:33:11] [nodemon] watching: server/app.js
[22:33:11] [nodemon] starting `node server/app.js`
[22:33:11] Finished 'nodemon' after 570 ms
[22:33:11] Starting 'browser-sync'...
[22:33:11] Finished 'browser-sync' after 20 ms
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE 127.0.0.1:3000
    at Object.exports._errnoException (util.js:890:11)
    at exports._exceptionWithHostPort (util.js:913:20)
    at Server._listen2 (net.js:1234:14)
    at listen (net.js:1270:10)
    at net.js:1379:9
    at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
[22:33:11] [nodemon] app crashed - waiting for file changes before starting...
[BS] Proxying: http://localhost:3000
[BS] Access URLs:
 ----------------------------------
       Local: http://localhost:4000
    External: http://10.0.1.28:4000
 ----------------------------------
          UI: http://localhost:3001
 UI External: http://10.0.1.28:3001
 ----------------------------------
[22:33:12] copy all files 16.03 MB
[22:33:12] Finished 'copy:domain.com' after 1.78 s
[22:33:12] Starting 'elements:domain.com'...
[22:33:12] Finished 'elements:domain.com' after 5.39 ms
[22:33:12] Starting 'jshint:domain.com'...
[22:33:12] Starting 'images:domain.com'...
[22:33:12] Starting 'fonts:domain.com'...
[22:33:12] Starting 'html:domain.com'...
[22:33:12] Finished 'images:domain.com' after 339 ms
[22:33:12] Finished 'fonts:domain.com' after 157 ms
[BS] Reloading Browsers...

这是我在触发 gulp 脚本运行之前和之后看到的:

Daniels-iMac:polymer dani$ lsof -i tcp:3000
Daniels-iMac:polymer dani$
Daniels-iMac:polymer dani$ lsof -i tcp:3000
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    95580 dani   19u  IPv4 0x8560662b0408bad3      0t0  TCP localhost:hbci (LISTEN)

看来端口 3000 上没有发生太多事情。我可以做些什么来解决这个错误?

【问题讨论】:

  • 看起来你已经在端口 3000 上运行了节点。你可以终止进程查看这个问题stackoverflow.com/questions/3855127/…
  • 我已经知道了。它要么不在端口 3000 上运行,要么在我运行 lsof -i tcp:3000 时得到一个 pid 结果,这取决于我是否运行 gulp。所以并不是说已经有另一个进程在运行,我尝试了其他几个不可能的端口:都产生相同的错误。看起来它更像是快速服务器以某种方式使用 nodemon 启动了两次。

标签: express gulp polymer nodemon


【解决方案1】:

我认为您在 http://localhost:3000 上多次使用相同的端口运行 browserSync,因为我假设 forEach 有多个路径。

【讨论】:

  • 不幸的是,这是负面的。在 nodemon 和 browser-sync 任务函数中触发 console.log 时,它们都只触发一次。仅将一项添加到 appsPath 数组的情况相同:问题仍然存在。这种行为符合预期,因为在运行 gulp.task 函数时,您只需定义任务本身。你不运行它。
  • d0h!虽然不完全是这样。我忘记了在我的 gulpfile 顶部运行服务器的试用 require 语句,因此被忽略了。感谢您的帮助威尔逊阿尔贝托
  • 很高兴您找到了根本原因! Gulp 可以使用一些更好的调试工具,这感觉像是可以通过记录任务来解决的问题之一
猜你喜欢
  • 1970-01-01
  • 2018-12-24
  • 2017-08-14
  • 2021-02-11
  • 2021-02-26
  • 2018-07-04
  • 2017-06-02
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多