We usually use emptyPort for this https://npmjs.org/package/empty-port and IIRC @lightsofapollo wrote some node util to check whether the randomly picked port is open...