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

Written by chantastic | Published 2023/09/27
Tech Story Tags: web-development | productivity | react | tutorial | javascript-development | zsh-function | context-switching | command-line-tools

TLDRI'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`.via the TL;DR App

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.


Written by chantastic | software explorer
Published by HackerNoon on 2023/09/27