Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
portfinder
Advanced tools
The portfinder npm package is a utility for getting a port number that is guaranteed to be free on the current machine. It is useful for running servers on dynamically assigned ports to avoid conflicts.
Get an available port
This feature allows you to find an available port on the machine. The `getPort` function takes a callback that provides the port number.
const portfinder = require('portfinder');
portfinder.getPort((err, port) => {
if (err) {
throw err;
}
console.log('Available port found:', port);
});
Set the base port
Before calling `getPort`, you can set the `basePort` property to tell portfinder to start searching for a free port from a certain number.
const portfinder = require('portfinder');
portfinder.basePort = 3000;
portfinder.getPort((err, port) => {
if (err) {
throw err;
}
console.log('Available port found:', port);
});
Set the range of ports to search
You can specify a range of ports for portfinder to search within by providing an options object with `port` and `stopPort` properties.
const portfinder = require('portfinder');
portfinder.getPort({
port: 3000, // minimum port
stopPort: 3333 // maximum port
}, (err, port) => {
if (err) {
throw err;
}
console.log('Available port found:', port);
});
Similar to portfinder, get-port is a package that helps you get an available TCP port to listen on. Unlike portfinder, get-port returns a Promise and supports async/await syntax, which can be more convenient in modern Node.js applications.
Detect-port is another package that checks for an available port on your machine. It is similar to portfinder but also allows you to check if a specific port is available and returns an alternative if it's not, which can be useful for suggesting the next available port.
$ npm install portfinder
The portfinder
module has a simple interface:
var portfinder = require('portfinder');
portfinder.getPort(function (err, port) {
//
// `port` is guaranteed to be a free port
// in this scope.
//
});
Or with promise (if Promise
s are supported) :
const portfinder = require('portfinder');
portfinder.getPortPromise()
.then((port) => {
//
// `port` is guaranteed to be a free port
// in this scope.
//
})
.catch((err) => {
//
// Could not get a free port, `err` contains the reason.
//
});
If portfinder.getPortPromise()
is called on a Node version without Promise (<4), it will throw an Error unless Bluebird or any Promise pollyfill is used.
By default portfinder
will start searching from 8000
and scan until maximum port number (65535
) is reached.
You can change this globally by setting:
portfinder.setBasePort(3000); // default: 8000
portfinder.setHighestPort(3333); // default: 65535
or by passing optional options object on each invocation:
portfinder.getPort({
port: 3000, // minimum port
stopPort: 3333 // maximum port
}, callback);
$ npm test
FAQs
A simple tool to find an open port on the current machine
The npm package portfinder receives a total of 5,817,043 weekly downloads. As such, portfinder popularity was classified as popular.
We found that portfinder 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
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.