Today I learned how to create a smart pm
alias that automatically picks the correct package manager based on the lockfile in the project. Super handy when jumping between pnpm
, yarn
, and npm
projects all day.
🧠The Alias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pm() {
if [ -f pnpm-lock.yaml ]; then
cmd="pnpm"
elif [ -f yarn.lock ]; then
cmd="yarn"
elif [ -f package-lock.json ]; then
cmd="npm"
else
echo "No lockfile found – falling back to yarn"
cmd="yarn"
fi
echo "Using $cmd"
$cmd "$@"
}