5353f4021531da99f582b04071f080da76b1c6e0580b638f42cecc9c8626aea09d067d25a327e35906d6e75fdf9e0493bdaab43caf8e1c770b1b7475534677 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # node-portfinder
  2. [![CI](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/http-party/node-portfinder/actions/workflows/ci.yml)
  3. ## Installation
  4. You can install `portfinder` using a package manager like npm, yarn, or bun:
  5. ``` bash
  6. npm install portfinder
  7. ```
  8. ## Usage
  9. The `portfinder` module has a simple interface:
  10. ``` js
  11. const portfinder = require('portfinder');
  12. portfinder.getPort(function (err, port) {
  13. //
  14. // `port` is guaranteed to be a free port
  15. // in this scope.
  16. //
  17. });
  18. ```
  19. Or using promises:
  20. ``` js
  21. const portfinder = require('portfinder');
  22. portfinder.getPortPromise()
  23. .then((port) => {
  24. //
  25. // `port` is guaranteed to be a free port
  26. // in this scope.
  27. //
  28. })
  29. .catch((err) => {
  30. //
  31. // Could not get a free port, `err` contains the reason.
  32. //
  33. });
  34. ```
  35. ### Ports search scope
  36. By default `portfinder` will start searching from `8000` and scan until maximum port number (`65535`) is reached.
  37. You can change this globally by setting:
  38. ```js
  39. portfinder.setBasePort(3000); // default: 8000
  40. portfinder.setHighestPort(3333); // default: 65535
  41. ```
  42. or by passing optional options object on each invocation:
  43. ```js
  44. portfinder.getPort({
  45. port: 3000, // minimum port
  46. stopPort: 3333 // maximum port
  47. }, callback);
  48. ```
  49. ## Run Tests
  50. ``` bash
  51. npm test
  52. ```
  53. #### Author: [Charlie Robbins][0]
  54. #### Author/Maintainer: [Erik Trom][1]
  55. #### License: MIT/X11
  56. [0]: http://nodejitsu.com
  57. [1]: https://github.com/eriktrom