You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can install `portfinder` using a package manager like npm, yarn, or bun:
8
+
5
9
```bash
6
-
$ npm install portfinder
10
+
npm install portfinder
7
11
```
8
12
9
13
## Usage
10
14
The `portfinder` module has a simple interface:
11
15
12
16
```js
13
-
var portfinder =require('portfinder');
17
+
constportfinder=require('portfinder');
18
+
19
+
portfinder.getPort(function (err, port) {
20
+
//
21
+
// `port` is guaranteed to be a free port
22
+
// in this scope.
23
+
//
24
+
});
25
+
```
26
+
27
+
Or with promise (if `Promise`s are supported) :
28
+
29
+
```js
30
+
constportfinder=require('portfinder');
14
31
15
-
portfinder.getPort(function (err, port) {
32
+
portfinder.getPortPromise()
33
+
.then((port) => {
16
34
//
17
35
// `port` is guaranteed to be a free port
18
36
// in this scope.
19
37
//
38
+
})
39
+
.catch((err) => {
40
+
//
41
+
// Could not get a free port, `err` contains the reason.
42
+
//
20
43
});
21
44
```
22
45
23
-
Or with promise (if `Promise`s are supported) :
24
-
25
-
```js
26
-
constportfinder=require('portfinder');
27
-
28
-
portfinder.getPortPromise()
29
-
.then((port) => {
30
-
//
31
-
// `port` is guaranteed to be a free port
32
-
// in this scope.
33
-
//
34
-
})
35
-
.catch((err) => {
36
-
//
37
-
// Could not get a free port, `err` contains the reason.
38
-
//
39
-
});
40
-
```
41
-
42
46
If `portfinder.getPortPromise()` is called on a Node version without Promise (<4), it will throw an Error unless [Bluebird](http://bluebirdjs.com/docs/getting-started.html) or any Promise pollyfill is used.
43
47
44
48
### Ports search scope
@@ -56,14 +60,14 @@ or by passing optional options object on each invocation:
0 commit comments