Find free port synchronously without callback
npm install find-free-port-syncLike config webpack-dev-server with port automatically.
``javascript`
module.exports = {
...
output: '...',
devServer: {
...
port: "a free port"
}
};
javascript
npm install find-free-port-sync --save-dev
`Options
start
type: number | default: 1Start of range to find, should be greater than 0
end
type: number | default: 65534End of range to find, should be less than 65535
num
type: number | default: 1Number of ports to find, relates to the return value
- When
num === 1, return a free random port if found, null if not
- When num > 1, return an array of free [port] orderly if found, empty array [] if notip
type: string | default: 0.0.0.0|127.0.0.1It will scan local adress by default, specify an ip here
port
type: number | default: nullIf port is defined, it will return whether the port is free around
start end ip option
Examples
Find a free port for local address
`javascript
let findFreePort = require('find-free-port-sync');let port = findFreePort();
`
Find 10 free ports between 10000 and 30000 for 192.168.1.1
`javascript
let findFreePort = require('find-free-port-sync');let port = findFreePort({
start: 10000,
end: 30000,
num: 10,
ip: '192.168.1.1'
});
`
Check if a port is free
`javascript
let findFreePort = require('find-free-port-sync');let portIsOk = findFreePort({
port: 12345
});
``