|
| 1 | +# 🔌 get-port-please |
| 2 | + |
| 3 | +Get an available TCP port to listen |
| 4 | + |
| 5 | +[![npm version][npm-version-src]][npm-version-href] |
| 6 | +[![npm downloads][npm-downloads-src]][npm-downloads-href] |
| 7 | +[![License][license-src]][license-href] |
| 8 | +[![JSDocs][jsdocs-src]][jsdocs-href] |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +Install package: |
| 13 | + |
| 14 | +```bash |
| 15 | +npm i get-port-please |
| 16 | +``` |
| 17 | + |
| 18 | +```js |
| 19 | +// ESM |
| 20 | +import { |
| 21 | + getPort, |
| 22 | + checkPort, |
| 23 | + getRandomPort, |
| 24 | + waitForPort, |
| 25 | +} from "get-port-please"; |
| 26 | + |
| 27 | +// CommonJS |
| 28 | +const { |
| 29 | + getPort, |
| 30 | + checkPort, |
| 31 | + getRandomPort, |
| 32 | + waitForPort, |
| 33 | +} = require("get-port-please"); |
| 34 | +``` |
| 35 | + |
| 36 | +``` |
| 37 | +getPort(options: GetPortOptions): Promise<number>; |
| 38 | +checkPort(port: number, host?: string): Promise<number | false> |
| 39 | +waitForPort(port: number, options): Promise<number | false> |
| 40 | +``` |
| 41 | + |
| 42 | +Try sequence is: port > ports > random |
| 43 | + |
| 44 | +## Options |
| 45 | + |
| 46 | +```ts |
| 47 | +interface GetPortOptions { |
| 48 | + name?: string; |
| 49 | + |
| 50 | + random?: boolean; |
| 51 | + port?: number; |
| 52 | + portRange?: [fromInclusive: number, toInclusive: number]; |
| 53 | + ports?: number[]; |
| 54 | + host?: string; |
| 55 | + |
| 56 | + memoDir?: string; |
| 57 | + memoName?: string; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### `name` |
| 62 | + |
| 63 | +Unique name for port memorizing. Default is `default`. |
| 64 | + |
| 65 | +### `random` |
| 66 | + |
| 67 | +If enabled, `port` and `ports` will be ignored. Default is `false`. |
| 68 | + |
| 69 | +### `port` |
| 70 | + |
| 71 | +First port to check. Default is `process.env.PORT || 3000` |
| 72 | + |
| 73 | +### `ports` |
| 74 | + |
| 75 | +Extended ports to check. |
| 76 | + |
| 77 | +### `portRange` |
| 78 | + |
| 79 | +Extended port range to check. |
| 80 | + |
| 81 | +The range's start and end are **inclusive**, i.e. it is `[start, end]` in the mathematical notion. |
| 82 | +Reversed port ranges are not supported. If `start > end`, then an empty range will be returned. |
| 83 | + |
| 84 | +### `alternativePortRange` |
| 85 | + |
| 86 | +Alternative port range to check as fallback when none of the ports are available. |
| 87 | + |
| 88 | +The range's start and end are **inclusive**, i.e. it is `[start, end]` in the mathematical notion. |
| 89 | +Reversed port ranges are not supported. If `start > end`, then an empty range will be returned. |
| 90 | + |
| 91 | +The default range is `[3000, 3100]` (only when `port` is unspecified). |
| 92 | + |
| 93 | +### `host` |
| 94 | + |
| 95 | +The host to check. Default is `process.env.HOST` otherwise all available hosts will be checked. |
| 96 | + |
| 97 | +## License |
| 98 | + |
| 99 | +MIT |
| 100 | + |
| 101 | +<!-- Badges --> |
| 102 | + |
| 103 | +[npm-version-src]: https://img.shields.io/npm/v/get-port-please?style=flat&colorA=18181B&colorB=F0DB4F |
| 104 | +[npm-version-href]: https://npmjs.com/package/get-port-please |
| 105 | +[npm-downloads-src]: https://img.shields.io/npm/dm/get-port-please?style=flat&colorA=18181B&colorB=F0DB4F |
| 106 | +[npm-downloads-href]: https://npmjs.com/package/get-port-please |
| 107 | +[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/get-port-please/main?style=flat&colorA=18181B&colorB=F0DB4F |
| 108 | +[codecov-href]: https://codecov.io/gh/unjs/get-port-please |
| 109 | +[license-src]: https://img.shields.io/github/license/unjs/get-port-please.svg?style=flat&colorA=18181B&colorB=F0DB4F |
| 110 | +[license-href]: https://github.com/unjs/get-port-please/blob/main/LICENSE |
| 111 | +[jsdocs-src]: https://img.shields.io/badge/jsDocs.io-reference-18181B?style=flat&colorA=18181B&colorB=F0DB4F |
| 112 | +[jsdocs-href]: https://www.jsdocs.io/package/get-port-please |
0 commit comments