import net from 'net'; /** * Check whether a local port can be bound (i.e. nothing is occupying it). * Unlike a connect-based probe, this also detects processes that hold the * port without accepting connections. * @author Jay Luker * @param port the local port to test * @returns true if the port is free */ const portIsFree = (port: number): Promise => { return new Promise((resolve) => { const server = net.createServer(); server.once('error', () => { resolve(false); }); server.once('listening', () => { server.close(() => { resolve(true); }); }); server.listen(port, '127.0.0.1'); }); }; export default portIsFree;