Last active
March 16, 2026 11:35
-
-
Save sidx1024/22b07c6dda9ca707d7b91e148d2e86ef to your computer and use it in GitHub Desktop.
npm -> pnpm redirector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # npm -> pnpm redirector | |
| # For those people who hate typing "pnpm" or even saying it | |
| # | |
| # INSTRUCTIONS | |
| # APPEND the following lines in our ~/.bashrc or ~/.zshrc | |
| ### npm -> pnpm shim start (https://gist.github.com/sidx1024/22b07c6dda9ca707d7b91e148d2e86ef) | |
| export REAL_NPM_BIN="$(command -v npm)" | |
| _find_pm_root() { | |
| local dir="${PWD}" | |
| while [ "$dir" != "/" ]; do | |
| if [ -f "$dir/pnpm-lock.yaml" ] || [ -f "$dir/pnpm-workspace.yaml" ]; then | |
| printf '%s\n' "$dir" | |
| return 0 | |
| fi | |
| dir="$(dirname "$dir")" | |
| done | |
| return 1 | |
| } | |
| _is_flag() { | |
| case "$1" in | |
| -*) return 0 ;; | |
| *) return 1 ;; | |
| esac | |
| } | |
| npm() { | |
| local real_npm="${REAL_NPM_BIN:-$(command -v npm)}" | |
| local pm_root | |
| if typeset -f _find_pm_root > /dev/null 2>&1; then | |
| pm_root="$(_find_pm_root)" || pm_root="" | |
| else | |
| pm_root="" | |
| fi | |
| if [ -z "$pm_root" ]; then | |
| command "$real_npm" "$@" | |
| return $? | |
| fi | |
| if [ $# -eq 0 ]; then | |
| echo "\033[33m↪ Redirecting to: pnpm\033[0m" >&2 | |
| command pnpm | |
| return $? | |
| fi | |
| local cmd="$1" | |
| shift | |
| case "$cmd" in | |
| install|i|add|uninstall|remove|rm|update|up|exec|x|run|test|start|stop|restart) | |
| for arg in "$@"; do | |
| case "$arg" in | |
| -g|--global) | |
| command "$real_npm" "$cmd" "$@" | |
| return $? | |
| ;; | |
| esac | |
| done | |
| ;; | |
| esac | |
| case "$cmd" in | |
| install|i) | |
| local has_pkg=0 | |
| for arg in "$@"; do | |
| if ! _is_flag "$arg"; then | |
| has_pkg=1 | |
| break | |
| fi | |
| done | |
| if [ "$has_pkg" -eq 1 ]; then | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm add $@\033[0m" >&2 | |
| command pnpm add "$@" | |
| else | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm install $@\033[0m" >&2 | |
| command pnpm install "$@" | |
| fi | |
| return $? | |
| ;; | |
| ci) | |
| echo "\033[33m↪ Redirecting: npm ci → pnpm install --frozen-lockfile\033[0m" >&2 | |
| command pnpm install --frozen-lockfile "$@" | |
| return $? | |
| ;; | |
| add) | |
| echo "\033[33m↪ Redirecting: npm add $@ → pnpm add $@\033[0m" >&2 | |
| command pnpm add "$@" | |
| return $? | |
| ;; | |
| uninstall|remove|rm) | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm remove $@\033[0m" >&2 | |
| command pnpm remove "$@" | |
| return $? | |
| ;; | |
| update|up) | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm update $@\033[0m" >&2 | |
| command pnpm update "$@" | |
| return $? | |
| ;; | |
| exec|x) | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm dlx $@\033[0m" >&2 | |
| command pnpm dlx "$@" | |
| return $? | |
| ;; | |
| run) | |
| echo "\033[33m↪ Redirecting: npm run $@ → pnpm run $@\033[0m" >&2 | |
| command pnpm run "$@" | |
| return $? | |
| ;; | |
| test) | |
| echo "\033[33m↪ Redirecting: npm test $@ → pnpm test $@\033[0m" >&2 | |
| command pnpm test "$@" | |
| return $? | |
| ;; | |
| start) | |
| echo "\033[33m↪ Redirecting: npm start $@ → pnpm start $@\033[0m" >&2 | |
| command pnpm start "$@" | |
| return $? | |
| ;; | |
| stop) | |
| echo "\033[33m↪ Redirecting: npm stop $@ → pnpm stop $@\033[0m" >&2 | |
| command pnpm stop "$@" | |
| return $? | |
| ;; | |
| restart) | |
| echo "\033[33m↪ Redirecting: npm restart $@ → pnpm restart $@\033[0m" >&2 | |
| command pnpm restart "$@" | |
| return $? | |
| ;; | |
| init) | |
| command "$real_npm" init "$@" | |
| return $? | |
| ;; | |
| publish|login|logout|whoami|owner|access|dist-tag|team|org|token|profile) | |
| command "$real_npm" "$cmd" "$@" | |
| return $? | |
| ;; | |
| *) | |
| echo "\033[33m↪ Redirecting: npm $cmd $@ → pnpm $cmd $@\033[0m" >&2 | |
| command pnpm "$cmd" "$@" | |
| command "$real_npm" "$cmd" "$@" | |
| return $? | |
| ;; | |
| esac | |
| } | |
| ### npm -> pnpm shim end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment