chan.dev / posts

A Universal Terminal alias for bun, pnpm, npm, and yarn

Screenshot of a text editor revealing a zsh function that determines the local package manager and runs the correct command. Works with bun, pnpm, npm, and yarn.

I’ve typed npm for the last time.

As JavaScript developers, we have four package managers to choose from. And between personal, work, and open source projects, I use every last one of them. This is a problem because typing the wrong command costs time and irritation.

Below is zsh function that I’ve used to eliminate package manager context switching heartache — typing npm start when I meant bun start or npm when I mean ::shudders:: yarn.

Terminal window
p() {
if [[ -f bun.lockb ]]; then
command bun "$@"
elif [[ -f pnpm-lock.yaml ]]; then
command pnpm "$@"
elif [[ -f yarn.lock ]]; then
command yarn "$@"
elif [[ -f package-lock.json ]]; then
command npm "$@"
else
command pnpm "$@"
fi
}

What this command does

zsh functions can be used like fancy aliases with the addition of a runtime check. This function checks for the existence of a lock file and runs the stated command with the correct package manager.

What this command does not do

This function does not create a universal interface around the varied package managers. I don’t find universal interfaces helpful.

And I enjoy utilizing the subtleties in each package manager. My problem is the muscle memory around common commands (start, install, and test) and those actions inadvertently spawn confusion.

Learn more

Check out these references if you want to learn more about zsh functions.