1 | # get-port [![Build Status](https://travis-ci.org/sindresorhus/get-port.svg?branch=master)](https://travis-ci.org/sindresorhus/get-port)
|
2 |
|
3 | > Get an available port
|
4 |
|
5 |
|
6 | ## Install
|
7 |
|
8 | ```
|
9 | $ npm install get-port
|
10 | ```
|
11 |
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | const getPort = require('get-port');
|
17 |
|
18 | getPort().then(port => {
|
19 | console.log(port);
|
20 | //=> 51402
|
21 | });
|
22 | ```
|
23 |
|
24 | Optionally, pass in a preferred port:
|
25 |
|
26 | ```js
|
27 | getPort({port: 3000}).then(port => {
|
28 | console.log(port);
|
29 | // Will use 3000 if available, otherwise fall back to a random port
|
30 | });
|
31 | ```
|
32 |
|
33 |
|
34 | ## API
|
35 |
|
36 | ### getPort([options])
|
37 |
|
38 | Returns a `Promise` for a port number.
|
39 |
|
40 | #### options
|
41 |
|
42 | Type: `Object`
|
43 |
|
44 | ##### port
|
45 |
|
46 | Type: `number`
|
47 |
|
48 | The preferred port to use.
|
49 |
|
50 | ##### host
|
51 |
|
52 | Type: `string`
|
53 |
|
54 | The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
|
55 |
|
56 |
|
57 | ## Related
|
58 |
|
59 | - [get-port-cli](https://github.com/sindresorhus/get-port-cli) - CLI for this module
|
60 |
|
61 |
|
62 | ## License
|
63 |
|
64 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|