0
# Port Configuration
1
2
Global configuration settings for portfinder operations including base port, highest port, and socket path defaults.
3
4
## Capabilities
5
6
### Base Port Property
7
8
The lowest port to begin any port search from. Defaults to 8000.
9
10
```javascript { .api }
11
/**
12
* The lowest port to begin any port search from
13
* @type {number}
14
* @default 8000
15
*/
16
portfinder.basePort: number;
17
```
18
19
**Usage Example:**
20
21
```javascript
22
const portfinder = require('portfinder');
23
24
// Check current base port
25
console.log('Current base port:', portfinder.basePort); // 8000
26
27
// Modify base port globally
28
portfinder.basePort = 3000;
29
30
// All subsequent port searches will start from 3000
31
portfinder.getPortPromise().then(port => {
32
console.log('Port found:', port); // e.g., 3000 or higher
33
});
34
```
35
36
### Set Base Port Function
37
38
Sets the lowest port to begin any port search from.
39
40
```javascript { .api }
41
/**
42
* Set the lowest port to begin any port search from
43
* @param {number} port - The new base port
44
* @returns {void}
45
*/
46
function setBasePort(port: number): void;
47
```
48
49
**Usage Example:**
50
51
```javascript
52
const portfinder = require('portfinder');
53
54
// Set base port using function
55
portfinder.setBasePort(5000);
56
57
console.log('Base port is now:', portfinder.basePort); // 5000
58
```
59
60
### Highest Port Property
61
62
The highest port to end any port search from. Defaults to 65535 (maximum possible port).
63
64
```javascript { .api }
65
/**
66
* The highest port to end any port search from
67
* @type {number}
68
* @default 65535
69
*/
70
portfinder.highestPort: number;
71
```
72
73
**Usage Example:**
74
75
```javascript
76
const portfinder = require('portfinder');
77
78
// Check current highest port
79
console.log('Current highest port:', portfinder.highestPort); // 65535
80
81
// Limit search range
82
portfinder.highestPort = 9000;
83
84
// Port searches will not exceed 9000
85
portfinder.getPort({ port: 8500 }, (err, port) => {
86
console.log('Port found in limited range:', port); // between 8500-9000
87
});
88
```
89
90
### Set Highest Port Function
91
92
Sets the highest port to end any port search from.
93
94
```javascript { .api }
95
/**
96
* Set the highest port to end any port search from
97
* @param {number} port - The new highest port
98
* @returns {void}
99
*/
100
function setHighestPort(port: number): void;
101
```
102
103
**Usage Example:**
104
105
```javascript
106
const portfinder = require('portfinder');
107
108
// Set highest port using function
109
portfinder.setHighestPort(8080);
110
111
console.log('Highest port is now:', portfinder.highestPort); // 8080
112
```
113
114
### Base Path Property
115
116
Default path to begin any socket search from. Defaults to '/tmp/portfinder'.
117
118
```javascript { .api }
119
/**
120
* Default path to begin any socket search from
121
* @type {string}
122
* @default '/tmp/portfinder'
123
*/
124
portfinder.basePath: string;
125
```
126
127
**Usage Example:**
128
129
```javascript
130
const portfinder = require('portfinder');
131
132
// Check current base path
133
console.log('Current base path:', portfinder.basePath); // '/tmp/portfinder'
134
135
// Change base path
136
portfinder.basePath = '/var/run/myapp';
137
138
// Socket searches will use new base path
139
portfinder.getSocketPromise().then(socketPath => {
140
console.log('Socket path:', socketPath); // e.g., '/var/run/myapp.sock'
141
});
142
```
143
144
### Set Base Path Function
145
146
Sets the base path to begin any socket search from.
147
148
```javascript { .api }
149
/**
150
* Set the base path to begin any socket search from
151
* @param {string} path - The new base path
152
* @returns {void}
153
*/
154
function setBasePath(path: string): void;
155
```
156
157
**Usage Example:**
158
159
```javascript
160
const portfinder = require('portfinder');
161
162
// Set base path using function
163
portfinder.setBasePath('/tmp/sockets');
164
165
console.log('Base path is now:', portfinder.basePath); // '/tmp/sockets'
166
```
167
168
### Default Hosts Array
169
170
Internal list of hostnames and IP addresses derived from network interfaces. This array is automatically populated and includes '0.0.0.0', all local interface addresses, and null (for binding without specifying a host).
171
172
```javascript { .api }
173
/**
174
* List of internal hostnames provided by your machine
175
* @type {string[]}
176
* @readonly
177
*/
178
portfinder._defaultHosts: string[];
179
```
180
181
**Usage Example:**
182
183
```javascript
184
const portfinder = require('portfinder');
185
186
// Inspect available hosts (read-only)
187
console.log('Available hosts:', portfinder._defaultHosts);
188
// Example output: ['0.0.0.0', '::1', '127.0.0.1', '192.168.1.100', null]
189
190
// Custom hosts are automatically added when used
191
portfinder.getPort({ host: '192.168.1.50' }, (err, port) => {
192
console.log('Updated hosts:', portfinder._defaultHosts);
193
// '192.168.1.50' is now included in the array
194
});
195
```
196
197
## Configuration Best Practices
198
199
### Global vs Per-Call Configuration
200
201
```javascript
202
const portfinder = require('portfinder');
203
204
// Global configuration affects all subsequent calls
205
portfinder.setBasePort(3000);
206
portfinder.setHighestPort(4000);
207
208
// Per-call options override global settings
209
portfinder.getPort({
210
port: 5000, // Overrides global basePort
211
stopPort: 6000 // Overrides global highestPort
212
}, callback);
213
214
// Global settings remain unchanged for other calls
215
portfinder.getPortPromise(); // Still uses basePort=3000, highestPort=4000
216
```
217
218
### Development vs Production Settings
219
220
```javascript
221
const portfinder = require('portfinder');
222
223
if (process.env.NODE_ENV === 'development') {
224
// Use standard development port range
225
portfinder.setBasePort(3000);
226
portfinder.setHighestPort(3999);
227
} else {
228
// Use higher port range for production
229
portfinder.setBasePort(8000);
230
portfinder.setHighestPort(9999);
231
}
232
```