Skip to main content
Deno 2 is finally here 🎉️
Learn more

getport deno doc

Get an available TCP port

Usage

import { getPort, randomPort, makeRange } from "https://deno.land/x/getport/mod.ts";
deno run --allow-net script.ts

Example

  • Without params

    import { getPort } from "https://deno.land/x/getport/mod.ts";
    const port = getPort();
    console.log(port); //=> Return first port available between 1024 and 65535
  • Pass in a preferred port:

    import { getPort } from "https://deno.land/x/getport/mod.ts";
    const port = await getPort(3000);
    console.log(port); //=> Will use 3000 if available, otherwise returns the port plus one
  • Pass in an array of preferred ports:

    import { getPort } from "https://deno.land/x/getport/mod.ts";
    const port = await getPort({ port: [3000, 3001, 3002] });
    console.log(port); //=> Will use once element in the preferred ports array if available, otherwise returns the last port in array plus one
  • Use the makeRange() helper in case you need a port in a certain range:

    import { getPort, randomPort, makeRange } from "https://deno.land/x/getport/mod.ts";
    const range = makeRange(3000, 3100);
    console.log(range); //=> Return [3000, 3002, ..., 3099, 3100]
    const port = await getPort(range);
    console.log(port); //=> Will use first available port between 3000 and 3100

API

  • getPort

    • @param port Port

    • @param hostname String

    • @return Number

      /**
       * Return available port.
       * @param {number | number[]} port
       * @param {string?} hostname
       * @return {number}
       */
      const port = getPort();
  • makeRange

    • @param from Number

    • @param to Number

    • @return Array

      /**
       * Create an array of number by min and max.
       * @param {number} from Must be between 1024 and 65535
       * @param {number} to Must be between 1024 and 65535 and greater than from
       * @return {number[]}
       */
      const port = makeRange();
  • randomPort

    • @param hostname String

    • @return Number

      /**
       * Return a random port between 1024 and 65535.
       * @param {string?} hostname
       * @return {number}
       */
      const port = randomPort();

Change Logs

See CHANGELOG.md