Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,48 @@
# node-portfinder [![CI](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml)
# node-portfinder

[![CI](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml)

## Installation

You can install `portfinder` using a package manager like npm, yarn, or bun:

``` bash
$ npm install portfinder
npm install portfinder
```

## Usage
The `portfinder` module has a simple interface:

``` js
var portfinder = require('portfinder');
const portfinder = require('portfinder');

portfinder.getPort(function (err, port) {
//
// `port` is guaranteed to be a free port
// in this scope.
//
});
```

Or with promise (if `Promise`s are supported) :

``` js
const portfinder = require('portfinder');

portfinder.getPort(function (err, port) {
portfinder.getPortPromise()
.then((port) => {
//
// `port` is guaranteed to be a free port
// in this scope.
//
})
.catch((err) => {
//
// Could not get a free port, `err` contains the reason.
//
});
```

Or with promise (if `Promise`s are supported) :

``` js
const portfinder = require('portfinder');

portfinder.getPortPromise()
.then((port) => {
//
// `port` is guaranteed to be a free port
// in this scope.
//
})
.catch((err) => {
//
// Could not get a free port, `err` contains the reason.
//
});
```

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.

### Ports search scope
Expand All @@ -56,14 +60,14 @@ or by passing optional options object on each invocation:

```js
portfinder.getPort({
port: 3000, // minimum port
stopPort: 3333 // maximum port
port: 3000, // minimum port
stopPort: 3333 // maximum port
}, callback);
```

## Run Tests
``` bash
$ npm test
npm test
```

#### Author: [Charlie Robbins][0]
Expand Down