Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
detect-port
Advanced tools
The detect-port npm package is a utility for detecting available network ports on a machine. It is useful for scenarios where you need to find an open port to start a server or service.
Detecting an available port
This feature allows you to check if a specific port is available. If the port is occupied, it suggests an alternative available port.
const detect = require('detect-port');
const port = 3000;
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}`);
}
});
Promise-based port detection
This feature provides a promise-based approach to detect an available port, making it easier to integrate with modern JavaScript code that uses async/await.
const detect = require('detect-port');
const port = 3000;
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);
});
The portfinder package is another utility for finding open ports. It offers similar functionality to detect-port but includes additional features like specifying a range of ports to search within. It is also promise-based and has a more extensive API.
The get-port package is a lightweight utility for finding an available port. It is similar to detect-port but focuses on simplicity and ease of use. It is promise-based and allows specifying a preferred port or a range of ports.
Node.js implementation of port detector
npm i detect-port
CommonJS
const { detect } = require('detect-port');
detect(port)
.then(realPort => {
if (port == realPort) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${realPort}`);
}
})
.catch(err => {
console.log(err);
});
ESM and TypeScript
import { detect } from 'detect-port';
detect(port)
.then(realPort => {
if (port == realPort) {
console.log(`port: ${port} was not occupied`);
} else {
console.log(`port: ${port} was occupied, try port: ${realPort}`);
}
})
.catch(err => {
console.log(err);
});
npm i detect-port -g
# get an available port randomly
$ detect
# detect pointed port
$ detect 80
# output verbose log
$ detect --verbose
# more help
$ detect --help
Most likely network error, check that your /etc/hosts
and make sure the content below:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Made with contributors-img.
FAQs
Node.js implementation of port detector
The npm package detect-port receives a total of 4,078,333 weekly downloads. As such, detect-port popularity was classified as popular.
We found that detect-port demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.