paint-brush
I'm Done Typing npm: A Zsh Function for JavaScript Package Managersby@chantastic
3,946 reads
3,946 reads

I'm Done Typing npm: A Zsh Function for JavaScript Package Managers

by chantasticSeptember 27th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

I've typed `npm` for the last time. This zsh function ensures that I don't spawn uneccassary heartache as I switch between projects — typing `npm start` when I meant `bun start` or `npm i` when I mant ::shudders:: `yarn`.
featured image - I'm Done Typing npm: A Zsh Function for JavaScript Package Managers
chantastic HackerNoon profile picture


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
}

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 spawning confusion.

Learn more

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

Intro to zsh functions

How to define and load your own shell function in zsh


Also published here.