get-port
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The "get-port" package is a handy tool for retrieving an available TCP port. It provides a simple, yet effective method for acquiring an available port on the user's system. If a specified preferred port is available, the package will provide it; otherwise, it defaults to a random available port. This can prove beneficial when configuring network-related tasks where an open port is required.
To use "get-port", start by installing it in your project using npm with the command npm install get-port
. Once installed, you can import it into your JavaScript file:
import getPort from 'get-port';
After importing the package, you can use it to fetch an available port. For instance, the following lines of code will return any available port on your system:
console.log(await getPort());
If you have a preferred port, pass it in as an option:
console.log(await getPort({port: 3000}));
The tool will use your preferred port if it's available. If not, it will fall back to any available port. You can even specify an array of preferred ports:
console.log(await getPort({port: [3000, 3001, 3002]}));
It's also possible to fetch a port within a specific range using the portNumbers()
helper:
import getPort, {portNumbers} from 'get-port';
console.log(await getPort({port: portNumbers(3000, 3100)}));
Keep in mind that while this package does its best to avoid race conditions within the same process, a small chance of a race condition exists if another process starts using the fetched port in between the time it's fetched and the time it's used.
The comprehensive and detailed documentation for the "get-port" package can be found in its README file available at the GitHub repository: get-port. It contains all the necessary details on how to install, use, configure options, and understand possible caveats when using this package.