What is detect-port?
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.
What are detect-port's main functionalities?
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);
});
Other packages similar to detect-port
portfinder
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.
get-port
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.
detect-port
Node.js implementation of port detector
Who are using or has used
For more
Usage
npm i detect-port
const detect = require('detect-port');
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);
});
Command Line Tool
npm i detect-port -g
Quick Start
$ detect
$ detect 80
$ detect --verbose
$ detect --help
FAQ
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
License
MIT
Contributors
Made with contributors-img.
2.0.0 (2024-12-08)
⚠ BREAKING CHANGES
- Drop Node.js < 16 support
- 使用 ts 重构
- 使用 tshy 支持 esm 和 cjs
- test 使用 test-runner (这里需要 node v18 版本)
merge from https://github.com/node-modules/detect-port/pull/51
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
Summary by CodeRabbit
-
New Features
-
Introduced a new waitPort
function to asynchronously wait for a
specified port to become available.
- Added a new ESLint configuration to enforce TypeScript linting rules.
-
Bug Fixes
- Reverted a feature in the
detect-port
package due to issues raised.
-
Documentation
- Updated
README.md
for improved clarity and updated badge links. - Modified
CONTRIBUTING.md
to reflect changes in testing commands.
-
Chores
- Introduced a new TypeScript configuration file (
tsconfig.json
).
-
Updated package.json
to reflect changes in dependencies and project
structure.
-
Tests
-
Added comprehensive tests for the new waitPort
and updated tests for
the CLI and detectPort
function.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Features
- refactor with typescript to support esm and cjs both (#56) (b5d32d2)