paint-brush
I'm Also Done Writing npm (Sort of)by@staticvoidmain
625 reads
625 reads

I'm Also Done Writing npm (Sort of)

by Jerónimo MileaOctober 18th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Are you weary of remembering which package manager you utilized for each project you wrote or tested? We have an effective solution.
featured image - I'm Also Done Writing npm (Sort of)
Jerónimo Milea HackerNoon profile picture

Are you weary of remembering which package manager you utilized for each project you wrote or tested? Do you crave a straightforward command that can "automagically" decide the appropriate one based on a found lock file, or use your preferred one when none is found?


We have an effective solution for Powershell and bash(ish) users, inspired by the excellent concept of @chantastic in https://hackernoon.com/im-done-typing-npm-a-zsh-function-for-javascript-package-managers. I ended up creating the same function for use with Powershell, which can be conveniently employed on both Linux and Windows. Here it is:


function p() {
  if (Test-Path -Path 'bun.lockb') {
    Write-Host -ForegroundColor Green "Using bun"
    & bun @($args)
  } elseif (Test-Path -Path 'pnpm-lock.yaml') {
    Write-Host -ForegroundColor Green "Using pnpm"
    & pnpm @($args)
  } elseif (Test-Path -Path 'yarn.lock') {
    Write-Host -ForegroundColor Green "Using yarn"
    & yarn @($args)
  } elseif (Test-Path -Path 'package-lock.json') {
    Write-Host -ForegroundColor Green "Using npm"
    & npm @($args)
  } else {
    Write-Host -ForegroundColor Green "Using pnpm"
    Write-Host -ForegroundColor Red "(No lock file found)"
    & pnpm @($args)
  }
}  

I incorporated some console prompts to remind us which package manager we're employing.

As evident, the function is quite straightforward yet remarkably powerful. This doesn't establish any universal interface for using package managers, as stated by @chantastic. Its chief aim is to simplify the routine use of these tools without the need to remember precisely which one was utilized for the current project.


I hope you find this solution intriguing and, more importantly, useful. If you make improvements, please share them so I can update this post!

Thank you for your time in reading this!