Skip to content

Instantly share code, notes, and snippets.

@marcosrocha85
Created November 28, 2025 12:51
Show Gist options
  • Select an option

  • Save marcosrocha85/5a9456757148ff0b5358d0249ab6c3e9 to your computer and use it in GitHub Desktop.

Select an option

Save marcosrocha85/5a9456757148ff0b5358d0249ab6c3e9 to your computer and use it in GitHub Desktop.
Intelligent alias for npm that auto-adds "run" when necessary for npm
# 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
}
@marcosrocha85
Copy link
Author

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:dev will run npm run start:dev or npm install will call the default npm install command. You still be able to call npm run commands if you like, what makes this alias very smart.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment