Node.js implementation of port detector
- ⭐⭐⭐eggjs/egg
- ⭐⭐⭐alibaba/ice
- ⭐⭐⭐alibaba/uirecorder
- ⭐⭐⭐facebook/create-react-app
- ⭐⭐⭐facebook/flipper
- ⭐⭐⭐umijs/umi
- ⭐⭐⭐gatsbyjs/gatsby
- ⭐⭐⭐electron-react-boilerplate/electron-react-boilerplate
- ⭐⭐⭐zeit/micro
- ⭐⭐⭐rails/webpacker
- ⭐⭐⭐storybookjs/storybook
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.