Created
November 28, 2025 12:51
-
-
Save marcosrocha85/5a9456757148ff0b5358d0249ab6c3e9 to your computer and use it in GitHub Desktop.
Intelligent alias for npm that auto-adds "run" when necessary for npm
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
| # Intelligent alias for npm that auto-adds "run" when necessary | |
| npm() { | |
| # If no arguments, run npm normally | |
| if [ $# -eq 0 ]; then | |
| command npm | |
| return | |
| fi | |
| local first_arg="$1" | |
| # List of native npm commands that do not require "run" | |
| local native_commands=( | |
| "install" "i" "uninstall" "remove" "rm" "update" "up" | |
| "init" "test" "t" "start" "stop" "restart" "version" | |
| "publish" "unpublish" "adduser" "login" "logout" "whoami" | |
| "link" "unlink" "outdated" "owner" "pack" "prune" "rebuild" | |
| "repo" "search" "view" "info" "show" "cache" "config" "get" | |
| "set" "help" "doctor" "audit" "fund" "ci" "exec" "x" | |
| "run" "run-script" "ls" "list" "ll" "la" | |
| ) | |
| # Check if it is a native command | |
| for cmd in "${native_commands[@]}"; do | |
| if [ "$first_arg" = "$cmd" ]; then | |
| command npm "$@" | |
| return | |
| fi | |
| done | |
| # If it starts with "-", it is a flag | |
| if [[ "$first_arg" =~ ^- ]]; then | |
| command npm "$@" | |
| return | |
| fi | |
| # Check if package.json exists | |
| if [ ! -f "package.json" ]; then | |
| command npm "$@" | |
| return | |
| fi | |
| # Check if the script exists in package.json | |
| if command npm run | grep -q "^ $first_arg$"; then | |
| command npm run "$@" | |
| else | |
| # Try to run directly (might be a command we haven't listed yet) | |
| command npm "$@" | |
| fi | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there. Are you using yarn just because "it's faster" and you can run commands without typing "run"? Did you know that npm received performance improvements and it's as good as yarn? Except for the need of "run" command. So I decided to create a bash alias that checks if the first parameter is a valid npm run command. If so, it calls npm with run your parameter. Like
npm start:devwill runnpm run start:devornpm installwill call the defaultnpm installcommand. You still be able to callnpm runcommands if you like, what makes this alias very smart.