
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
tcp-port-used
Advanced tools
The tcp-port-used npm package is a utility for checking the status of TCP ports. It allows you to determine if a specific port is in use, wait for a port to become available, or wait for a port to be used. This can be particularly useful for network-related tasks, such as ensuring that a server has started or stopped correctly.
Check if a port is in use
This feature allows you to check if a specific TCP port is currently in use. The code sample demonstrates how to check if port 8080 is in use and logs the result.
const tcpPortUsed = require('tcp-port-used');
const port = 8080;
tcpPortUsed.check(port)
.then(inUse => {
console.log(`Port ${port} is ${inUse ? 'in use' : 'free'}`);
})
.catch(err => {
console.error('Error on check:', err.message);
});
Wait for a port to become available
This feature allows you to wait until a specific TCP port becomes available. The code sample demonstrates how to wait until port 8080 is free, checking every 500ms for up to 30 seconds.
const tcpPortUsed = require('tcp-port-used');
const port = 8080;
tcpPortUsed.waitUntilFree(port, 500, 30000)
.then(() => {
console.log(`Port ${port} is now free`);
})
.catch(err => {
console.error('Error on waitUntilFree:', err.message);
});
Wait for a port to be used
This feature allows you to wait until a specific TCP port is in use. The code sample demonstrates how to wait until port 8080 is in use, checking every 500ms for up to 30 seconds.
const tcpPortUsed = require('tcp-port-used');
const port = 8080;
tcpPortUsed.waitUntilUsed(port, 500, 30000)
.then(() => {
console.log(`Port ${port} is now in use`);
})
.catch(err => {
console.error('Error on waitUntilUsed:', err.message);
});
The portscanner package provides similar functionality for checking if a port is in use. It also allows you to find an open port within a range. Compared to tcp-port-used, portscanner offers more flexibility in scanning a range of ports but lacks the waiting functionality.
The detect-port package is used to detect an open port on the system. It is simpler and more focused on finding an available port rather than checking or waiting for a specific port's status. It is less feature-rich compared to tcp-port-used.
The is-port-reachable package checks if a port is reachable on a given host. It is useful for network diagnostics and testing connectivity. While it provides similar functionality to tcp-port-used in terms of checking port status, it is more focused on reachability over a network.
A simple Node.js module to check if a TCP localhost port is currently in use. It returns a promise from the q library.
Note: You have to admin privs to successfully test system ports (0-1023).
npm install tcp-port-used
To check a port's state:
var tcpPortUsed = require('tcp-port-used');
tcpPortUsed.check(44201)
.then(function(inUse) {
console.log('Port 44201 usage: '+inUse);
}, function(err) {
console.error('Error on check: '+err.message);
});
tcpPortUsed.waitUntilFree(44201, 500, 4000)
.then(function() {
console.log('Port 44201 is now available!');
}, function(err) {
console.error(err.message);
});
To wait until a port is available:
tcpPortUsed.waitUntilFree(44203, 500, 4000)
.then(function() {
console.log('Port 44203 is now free.');
}, function(err) {
console.loh('Error: ', error.message);
});
To wait until a port is accepting connections:
tcpPortUsed.waitUntilUsed(44204, 500, 4000)
.then(function() {
console.log('Port 44204 is now in use.');
}, function(err) {
console.loh('Error: ', error.message);
});
Checks if a TCP port is in use by creating the socket and binding it to the target port. Once bound, successfully, it's assume the port is availble. After the socket is closed or in error, the promise is resolved. Note: you have to be super user to correctly test system ports (0-1023).
Param:
Number port The port you are curious to see if available.
Returns:
Object A deferred Q promise.
Creates a deferred promise and fulfills it only when the socket is free. Will retry on an interval specified in retryTimeMs. Note: you have to be super user to correctly test system ports (0-1023).
Params:
Returns:
Object A deferred Q promise.
Creates a deferred promise and fulfills it only when the socket is used. Will retry on an interval specified in retryTimeMs. Note: you have to be super user to correctly test system ports (0-1023).
Params:
Returns:
Object A deferred Q promise.
The MIT License (MIT)
Copyright (c) 2013 jut-io
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A simple Node.js module to check if a TCP port is already bound.
The npm package tcp-port-used receives a total of 1,709,235 weekly downloads. As such, tcp-port-used popularity was classified as popular.
We found that tcp-port-used demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.