is-port-open
v1.0.0
Published
Check if a port is open on a given host
Maintainers
Readme
is-port-open
A simple and lightweight Node.js package to check if a port is open on a given host using async socket connections.
Installation
npm install is-port-openUsage
const isPortOpen = require('is-port-open');
// Check if port 80 is open on localhost
isPortOpen(80)
.then(isOpen => {
console.log(`Port 80 is ${isOpen ? 'open' : 'closed'}`);
});
// Check if port 443 is open on a specific host
isPortOpen(443, 'google.com')
.then(isOpen => {
console.log(`Port 443 is ${isOpen ? 'open' : 'closed'}`);
});
// With custom timeout (in milliseconds)
isPortOpen(8080, 'localhost', 5000)
.then(isOpen => {
console.log(`Port 8080 is ${isOpen ? 'open' : 'closed'}`);
});
// Using async/await
async function checkPort() {
const isOpen = await isPortOpen(3000);
console.log(`Port 3000 is ${isOpen ? 'open' : 'closed'}`);
}API
isPortOpen(port, host?, timeout?)
Returns a Promise that resolves to true if the port is open, false otherwise.
Parameters
port(number, required): Port number to check (1-65535)host(string, optional): Host to check. Default:'localhost'timeout(number, optional): Timeout in milliseconds. Default:2000
Returns
Promise<boolean>:trueif port is open,falseif closed or unreachable
Throws
Error: If port number is invalid (not between 1 and 65535)
Use Cases
Perfect for CLI tools and scripts that need to:
- Check if a service is running before starting
- Verify port availability before binding
- Monitor service health
- Test network connectivity
License
MIT
