Giter Club home page Giter Club logo

detect-port's Introduction

logo


NPM version build status Test coverage npm download

Node.js implementation of port detector

Who are using or has used

For more

Usage

$ npm i detect-port --save
const detect = require('detect-port');
/**
 * use as a promise
 */

detect(port)
  .then(_port => {
    if (port == _port) {
      console.log(`port: ${port} was not occupied`);
    } else {
      console.log(`port: ${port} was occupied, try port: ${_port}`);
    }
  })
  .catch(err => {
    console.log(err);
  });

Command Line Tool

$ npm i detect-port -g

Quick Start

# get an available port randomly
$ detect

# detect pointed port
$ detect 80

# output verbose log
$ detect --verbose

# more help
$ detect --help

FAQ

Most likely network error, check that your /etc/hosts and make sure the content below:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

Contributors


xudafeng


fengmk2


ziczhu


gaearon


chnliquan


popomore


snapre


yavuzakyuz


antife-yinyue

This project follows the git-contributor spec, auto updated at Wed Sep 21 2022 23:10:27 GMT+0800.

License

MIT

detect-port's People

Contributors

antife-yinyue avatar chnliquan avatar fengmk2 avatar gaearon avatar popomore avatar semantic-release-bot avatar snapre avatar xudafeng avatar yavuzakyuz avatar ziczhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

detect-port's Issues

Warnings displayed when running detect-port

After upgrading to detect-port v. 1.5.0 (and I see the same issue with 1.4.0), I get the following messages:

> ng serve main --port `detect 8080`

C:\xxx\node_modules\.bin/../detect-port/bin/detect-port.js: line 1: use strict: command not found
C:\xxx\node_modules\.bin/../detect-port/bin/detect-port.js: line 3: syntax error near unexpected token `('
C:\xxx\node_modules\.bin/../detect-port/bin/detect-port.js: line 3: `const pkg = require('../package');'

Running an Angular project on Windows 10.

I reverted to 1.3.0 which works fine for my purposes.

Side notes: the HISTORY file doesn't list the 1.4.0 version. And the Releases side panel on the main page still lists the 1.2.0 release… (I have no idea how this is updated.)

All port was occupied ?

I find that all ports are occupied when use this package to detect the port occupancy of my computer。

such as:

image

Host:

image

Env:

macOS: 10.13.4

Other Case:

端口检测程序是否有问题?

'use strict';

const debug = require('debug')('detect-port');
const net = require('net');

module.exports = (port, callback) => {
  if (typeof port === 'function') {
    callback = port;
    port = null;
  }
  if (typeof callback === 'function') {
    return tryListen(port, callback);
  }
  // promise
  return new Promise(resolve => {
    tryListen(port, (_, realPort) => {
      resolve(realPort);
    });
  });
};

function tryListen(port, callback) {
  port = parseInt(port) || 0;
  const server = new net.Server();

  server.on('error', err => {
    debug('listen %s error: %s', port, err);
    port = 0;
    server.close();
    return tryListen(port, callback);
  });

  server.listen({ port }, () => {
    port = server.address().port;
    server.close();
    debug('get free port: %s', port);
    callback(null, port);
  });
}

对于这部分代码

  server.listen({ port }, () => {
    port = server.address().port;
    server.close();
    debug('get free port: %s', port);
    callback(null, port);
  });

是否有问题,这个检测端口的程序的逻辑是先占用端口,如果占用成功,再释放掉,证明端口可用,如果同时调用两次detect-port(相同端口),会不会存在第一个占用成功然后释放的过程中,第二个程序又检测到这个端口可用然后返回相同的端口可用?能否帮忙看下@fengmk2 所以正确的逻辑是不是应该不占用端口,而是直接连接该端口看是否连接成功,如果失败,则证明端口可用。

Claims all ports occupied on macOS high sierra

For every port I've tried detect-port claims the port is occupied but lsof finds nothing. Turning on DEBUG shows errors similar to

detect-port listen localhost:3009 error: Error: getaddrinfo ENOTFOUND localhost +1ms

It appears default macos installation does not include localhost in /etc/hosts -- the file is completely empty. Adding an entry fixes this, but IMO it shouldn't be required.

macOS 10.13.3

detect error when localhost is not binding

detect-port detect free port between [8003, 8013) +0ms
  detect-port get free null:8003 +7ms
  detect-port listen localhost:8003 error: Error: getaddrinfo ENOTFOUND localhost +5ms
  detect-port get free null:8004 +1ms
  detect-port listen localhost:8004 error: Error: getaddrinfo ENOTFOUND localhost +2ms
  detect-port get free null:8005 +0ms
  detect-port listen localhost:8005 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8006 +0ms
  detect-port listen localhost:8006 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8007 +0ms
  detect-port listen localhost:8007 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8008 +0ms
  detect-port listen localhost:8008 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8009 +0ms
  detect-port listen localhost:8009 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8010 +0ms
  detect-port listen localhost:8010 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8011 +0ms
  detect-port listen localhost:8011 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port get free null:8012 +0ms
  detect-port listen localhost:8012 error: Error: getaddrinfo ENOTFOUND localhost +1ms
  detect-port port: 8013 >= maxPort: 8013, give up and use random port +0ms
  detect-port get free null:56846 +0ms
port 8003 was occupied
get available port 56846

Library not detecting port is in use

I'm using this lib with Gatsby having borrowed the idea from create-react-app. It had been working great but it's now for whatever reason not detecting used ports.

I start a gatsby instance on port 8000 and then another and detect-port tells me 8000 is open but when gatsby tries starting in the second instance, it gets an EADDRINUSE.

I've tried this on node 6 and 7.

Does it matter that Gatsby is listening on 0.0.0.0?

A problem with an installation via NPM

Hi.
I don't know where to write about this problem. Hope, you know how to solve it.
I try to install "detect-port-alt" via NMP but it fails always.
No another package has this problem.

21645 verbose stack Error: ENOENT: no such file or directory, chmod '/mnt/storage/all/work/libraries/benchmark-react/node_modules/detect-port-alt/bin/detect-port'
21646 verbose cwd /mnt/storage/all/work/libraries/benchmark-react
21647 verbose Linux 4.13.0-36-generic
21648 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
21649 verbose node v9.2.0
21650 verbose npm  v5.7.1
21651 error path /mnt/storage/all/work/libraries/benchmark-react/node_modules/detect-port-alt/bin/detect-port
21652 error code ENOENT
21653 error errno -2
21654 error syscall chmod
21655 error enoent ENOENT: no such file or directory, chmod '/mnt/storage/all/work/libraries/benchmark-react/node_modules/detect-port-alt/bin/detect-port'
21656 error enoent This is related to npm not being able to find a file.
21657 verbose exit [ -2, true ]

Is it possible to use next available port?

Before updating to 1.1.0, detect-port used to suggest the next available port (e.g. 3001 if I requested 3000 but it is busy). Now, it seems to always suggest random ports (e.g. I just got 49655).

Is it possible to preserve the old behavior of suggesting the next available port, or are there any downsides?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.