Installations
npm install get-port
Score
99.4
Supply Chain
99.5
Quality
76.8
Maintenance
100
Vulnerability
100
License
Developer
sindresorhus
Developer Guide
Module System
ESM
Min. Node Version
>=16
Typescript Support
Yes
Node Version
18.19.1
NPM Version
9.2.0
Statistics
877 Stars
67 Commits
64 Forks
15 Watching
1 Branches
21 Contributors
Updated on 28 Nov 2024
Bundle Size
2.40 kB
Minified
1.15 kB
Minified + Gzipped
Languages
JavaScript (95.19%)
TypeScript (4.81%)
Total Downloads
Cumulative downloads
Total Downloads
1,467,095,666
Last day
-20.7%
1,503,421
Compared to previous day
Last week
-2.5%
9,888,566
Compared to previous week
Last month
5.6%
42,242,320
Compared to previous month
Last year
29.9%
460,980,104
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
get-port
Get an available TCP port.
Install
1npm install get-port
Usage
1import getPort from 'get-port'; 2 3console.log(await getPort()); 4//=> 51402
Pass in a preferred port:
1import getPort from 'get-port'; 2 3console.log(await getPort({port: 3000})); 4// Will use 3000 if available, otherwise fall back to a random port
Pass in an array of preferred ports:
1import getPort from 'get-port'; 2 3console.log(await getPort({port: [3000, 3001, 3002]})); 4// Will use any element in the preferred ports array if available, otherwise fall back to a random port
Use the portNumbers()
helper in case you need a port in a certain range:
1import getPort, {portNumbers} from 'get-port'; 2 3console.log(await getPort({port: portNumbers(3000, 3100)})); 4// Will use any port from 3000 to 3100, otherwise fall back to a random port
API
getPort(options?)
Returns a Promise
for a port number.
options
Type: object
port
Type: number | Iterable<number>
A preferred port or an iterable of preferred ports to use.
exclude
Type: Iterable<number>
Ports that should not be returned.
You could, for example, pass it the return value of the portNumbers()
function.
host
Type: string
The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address.
By default, it checks availability on all local addresses defined in OS network interfaces. If this option is set, it will only check the given host.
portNumbers(from, to)
Generate port numbers in the given range from
...to
.
Returns an Iterable
for port numbers in the given range.
from
Type: number
The first port of the range. Must be in the range 1024
...65535
.
to
Type: number
The last port of the range. Must be in the range 1024
...65535
and must be greater than from
.
clearLockedPorts()
Clear the internal cache of locked ports.
This can be useful when you want the results to be unaffected by previous calls.
Please note that clearing the cache could cause race conditions.
1import getPort, {clearLockedPorts} from 'get-port'; 2 3const port = [3000, 3001, 3002]; 4 5console.log(await getPort({port})); 6//=> 3000 7 8console.log(await getPort({port})); 9//=> 3001 10 11// If you want the results to be unaffected by previous calls, clear the cache. 12clearLockedPorts(); 13 14console.log(await getPort({port})); 15//=> 3000
Beware
There is a very tiny chance of a race condition if another process starts using the same port number as you in between the time you get the port number and you actually start using it.
Race conditions in the same process are mitigated against by using a lightweight locking mechanism where a port will be held for a minimum of 15 seconds and a maximum of 30 seconds before being released again.
Related
- get-port-cli - CLI for this module
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
- Info: security policy file detected: .github/security.md:1
- Info: Found linked content: .github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/security.md:1
- Info: Found text in security policy: .github/security.md:1
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 13/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/get-port/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/get-port/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:26
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Score
4.4
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More