
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
detect-port-alt
Advanced tools
JavaScript Implementation of Port Detector
$ npm i detect-port --save
const detect = require('detect-port');
/**
 * callback usage
 */
detect(port, (err, _port) => {
  if (err) {
    console.log(err);
  }
  if (port == _port) {
    console.log(`port: ${port} was not occupied`);
  } else {
    console.log(`port: ${port} was occupied, try port: ${_port}`);
  }
});
/**
 * for a yield syntax instead of callback function implement
 */
const co = require('co');
co(function *() {
  const _port = yield detect(port);
  if (port == _port) {
    console.log(`port: ${port} was not occupied`);
  } else {
    console.log(`port: ${port} was occupied, try port: ${_port}`);
  }
});
/**
 * use as a promise
 */
detect(port)
  .then(_port => {
    if (port == _port) {
      console.log(`port: ${port} was not occupied`);
    } else {
      console.log(`port: ${port} was occupied, try port: ${_port}`);
    }
  })
  .catch(err => {
    console.log(err);
  });
$ npm i detect-port -g
# get an available port randomly
$ detect
# detect pointed port
$ detect 80
# more help
$ detect --help
Portfinder is a popular npm package that also helps in finding an open port on the current machine. Unlike detect-port-alt, portfinder provides more configuration options such as setting a range of ports to check. This can be particularly useful when you need to find multiple free ports within a specific range.
Get-port is another similar package that provides an easy-to-use API to get an available port. It's very minimalistic compared to detect-port-alt and does not provide callback support, focusing solely on promises, which makes it suitable for use in applications fully based on modern JavaScript features.
FAQs
detect available port
We found that detect-port-alt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.