npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

acquire-port

v0.3.14

Published

Acquire a consistent guaranteed-to-be-free port number for your dev server or whatever else, no memorizing required!

Downloads

8

Readme

npm
version

acquire-port

BEFORE acquire-port:

# Project 1
$ next -p 3004

# Project 2
$ gatsby develop -p 3005

# Project 3
$ node expressapp.js -p 3006

# A week later, starting a new project...
# Project 4
# Did I already use port 3005? Or was the next one 3006?
$ ./koaapp.js --port 3006 # Oops!

# Open another terminal, want to run second instance, up arrow + enter, and...
$ ./koaapp.js --port 3006 # Guaranteed EADDRINUSE!

AFTER acquire-port:

# Optional: install acquire-port in your path
$ npm install -g acquire-port

# Project 1
$ next -p `acquire-port proj1` # Port A

# Project 2
$ gatsby develop -p `ap proj2` # Port B
# `ap` is synonymous with `acquire-port`!

# Project 3
$ node expressapp.js -p `npx -q acquire-port p3` # Or don't install acquire-port at all

# A week later, starting a new project...
# Project 4
$ ./koaapp.js --port `ap project-4` # Port D

# Close the project... come back to it a few weeks later
$ ./koaapp.js --port `ap project-4` # Port D (same port as before)

# And in another window, we want to start another dev instance without problems
$ ./koaapp.js --port `ap project-4` # Will run on the next available port temporarily

# You don't even need an id. Call acquire-port by itself and it'll use the directory name
# as the id automatically. Easy peasy!
$ cd ~/repos/freelance/shiny-new-react-app
$ npm run dev -p `ap`
# Same as: npm run dev -p `acquire-port shiny-new-react-app`

acquire-port takes in an identifier (id) and spits out a mapped port number. Subsequent calls using the same id will get the same port number again and again. The only exception is when the originally mapped port is being used by another process, in which case acquire-port temporarily returns the next least unused port using detect-port. Once the originally mapped port is no longer in use, later calls with the same id will return it.

Port numbers start at 3000. You can change/delete your port mappings and/or choose the next starting port in the _portmap.json configuration file found at ~/.config/_portmap.json.

This is useful if, like me, you're running any dozen dev servers off the same rig simultaneously and like to have consistent port numbers for your projects with the added flexibility of temporary port assignments when necessary. I use it in my package.json files, composer scripts, shell scripts, and other places like so:

{
    ...
    "scripts": {
        "dev": "next -p `acquire-port`"
    }
}

Or with npx, so users who don't have acquire-port installed globally can still call npm run dev without an issue:

{
    ...
    "scripts": {
        "dev": "next -p `npx -q acquire-port`"
    }
}

Note: using npx might add overhead to your program's start time. To make npx -q acquire-port return instantaneously, ensure acquire-port is installed globally.

Installation and Usage

$ npm install -g acquire-port
$ acquire-port ident
$ port ident
$ port

This tool can also be used via npx without installing anything:

$ echo `npx acquire-port ident`

Though, this will add a few seconds to your program's start time. To avoid this, ensure acquire-port is installed globally.