@fnet/free-port v0.1.3
@fnet/free-port
The @fnet/free-port
project is a straightforward utility designed to help users identify available network ports on their local machine. This tool is useful for developers and system administrators who need to quickly find open ports for running applications or services without conflicts.
How It Works
The utility operates by checking specified ports or ranges of ports on a local computer to determine whether they are in use. It uses platform-specific commands to verify port availability, supporting both Windows and Unix-like systems. Users can request a certain number of free ports, and the tool will return a list of available ports up to that specified count.
Key Features
- Port Range Checking: Allows users to specify individual ports or a range of ports to check for availability.
- Adjustable Count: Users can request multiple available ports by specifying the desired count.
- Cross-Platform Support: Compatible with both Windows and Unix-based systems.
Conclusion
The @fnet/free-port
utility provides a simple and effective way to find available network ports on your machine, aiding in tasks such as setting up new services or applications that require free port assignments. It contributes to a smoother workflow by quickly resolving port availability issues.
Developer Guide for @fnet/free-port
Overview
@fnet/free-port
is a Node.js library designed to help developers find available network ports on their local machines. This can be particularly useful for applications that need to dynamically allocate ports for services or testing purposes. The library efficiently checks for port availability and returns a specified number of free ports, making it an essential tool for network configuration and management tasks within your applications.
Installation
To use @fnet/free-port
in your project, you can install it via npm or yarn:
npm install @fnet/free-port
or
yarn add @fnet/free-port
Usage
@fnet/free-port
provides a simple API to find available ports. You can specify a list of specific ports or ranges and the number of ports you need. The library will return a promise that resolves with a list of available ports.
Here is how you can use the library in your application:
import findFreePorts from '@fnet/free-port';
async function getAvailablePorts() {
try {
const availablePorts = await findFreePorts({
ports: [[3000, 4000], 8080], // Specify ranges or specific ports
count: 3 // Specify the number of available ports you need
});
console.log('Available ports:', availablePorts);
} catch (error) {
console.error('Error finding free ports:', error);
}
}
getAvailablePorts();
Examples
Example 1: Find a single available port
If you need just one available port, you can call the function without any arguments:
import findFreePorts from '@fnet/free-port';
findFreePorts().then(ports => {
console.log(`Found available port: ${ports[0]}`);
}).catch(error => {
console.error('Error finding port:', error);
});
Example 2: Find multiple available ports within specific ranges
Specify port ranges and the exact number of free ports you need:
import findFreePorts from '@fnet/free-port';
findFreePorts({ ports: [[8000, 8100], [9000, 9100]], count: 5 }).then(ports => {
console.log('Available ports:', ports);
}).catch(error => {
console.error('Error finding ports:', error);
});
These examples illustrate typical usage patterns: finding a single available port or multiple ports within specified ranges.
Acknowledgement
None required as per the project's guidelines.
This guide provided an overview of how to use @fnet/free-port
effectively in your projects. By following the installation and usage sections, developers can easily integrate port-finding functionality into their Node.js applications.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
ports:
description: List of specific ports or ranges to check for availability. If a
range is specified, ports within that range are checked.
type: array
items:
oneOf:
- type: number
- type: array
items:
type: number
minItems: 2
maxItems: 2
default:
- - 32767
- 65535
count:
description: The number of available ports to find.
type: number
default: 1
required: []