# detect-port

> Node.js implementation of port detector

Latest version **2.1.0** (published 2024-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install detect-port
pnpm add detect-port
yarn add detect-port
bun add detect-port
```

Provides the commands `detect`, `detect-port`.

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-12-10 |
| First published | 2015-05-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 16.0.0 |
| Dependencies | 1 |
| Unpacked size | 54 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 392 |
| Maintainers | fengmk2, xudafeng, ziczhu |
| Keywords | detect, port |

## Links

- npm: https://www.npmjs.com/package/detect-port
- Repository: https://github.com/node-modules/detect-port
- Issues: https://github.com/node-modules/detect-port/issues
- npm.io page: https://npm.io/package/detect-port

## Dependencies (1)

- [address](https://npm.io/package/address.md) ^2.0.1

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-12-10
- 1.6.1 (release-1.x) — 2024-05-08
- 2.0.1 — 2024-12-08
- 2.0.0 — 2024-12-08
- 1.6.0 — 2024-05-08
- 1.5.1 — 2022-09-22
- 1.5.1-beta — 2022-09-22
- 1.5.0 — 2022-09-21
- 1.4.1 — 2022-09-21
- 1.4.0 — 2022-09-21
- 1.3.0 — 2018-11-20
- 1.2.3 — 2018-05-16
- 1.2.2 — 2017-11-05
- 1.2.1 — 2017-06-12
- 1.2.0 — 2017-06-06
- … 18 more at https://npm.io/package/detect-port/versions

## README

# detect-port

[![NPM version][npm-image]][npm-url]
[![CI](https://github.com/node-modules/detect-port/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/detect-port/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version][node-version-image]][node-version-url]

[npm-image]: https://img.shields.io/npm/v/detect-port.svg?style=flat-square
[npm-url]: https://npmjs.org/package/detect-port
[codecov-image]: https://codecov.io/gh/node-modules/detect-port/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/detect-port
[snyk-image]: https://snyk.io/test/npm/detect-port/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/detect-port
[download-image]: https://img.shields.io/npm/dm/detect-port.svg?style=flat-square
[download-url]: https://npmjs.org/package/detect-port
[node-version-image]: https://img.shields.io/node/v/detect-port.svg?style=flat-square
[node-version-url]: https://nodejs.org/en/download/

> Node.js implementation of port detector

## Who are using or has used

- ⭐⭐⭐[eggjs/egg](//github.com/eggjs/egg)
- ⭐⭐⭐[alibaba/ice](//github.com/alibaba/ice)
- ⭐⭐⭐[alibaba/uirecorder](//github.com/alibaba/uirecorder)
- ⭐⭐⭐[facebook/create-react-app](//github.com/facebook/create-react-app/blob/main/packages/react-dev-utils/package.json)
- ⭐⭐⭐[facebook/flipper](//github.com/facebook/flipper)
- ⭐⭐⭐[umijs/umi](//github.com/umijs/umi)
- ⭐⭐⭐[gatsbyjs/gatsby](//github.com/gatsbyjs/gatsby)
- ⭐⭐⭐[electron-react-boilerplate/electron-react-boilerplate](//github.com/electron-react-boilerplate/electron-react-boilerplate)
- ⭐⭐⭐[zeit/micro](//github.com/zeit/micro)
- ⭐⭐⭐[rails/webpacker](//github.com/rails/webpacker)
- ⭐⭐⭐[storybookjs/storybook](//github.com/storybookjs/storybook)

[For more](//github.com/node-modules/detect-port/network/dependents)

## Usage

```bash
npm i detect-port
```

CommonJS

```javascript
const { detect } = require('detect-port');

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

ESM and TypeScript

```ts
import { detect } from 'detect-port';

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

## Command Line Tool

```bash
npm i detect-port -g
```

### Quick Start

```bash
# 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:

```bash
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
```

## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=node-modules/detect-port)](https://github.com/node-modules/detect-port/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).

---
_Source: https://npm.io/package/detect-port · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
