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 a zsh function that I've used to eliminate package manager context switching heartache — typing npm start when we meant bun start or npm I when we mean **shudders** yarn.
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
}
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 spawning confusion.
Check out these references if you want to learn more about Zsh functions.
How to define and load your own shell function in zsh
Also published here.