I added this to my [.bashrc][1] file:
function npmv {
case $# in # Number of arguments passed
0) v="$(npm -v)" ; # Store output from npm -v in variable
echo "NPM version is: $v"; # Can't use single quotes
# ${v} would also work
;;
1) s="$(npm list --depth=0 $1 | grep $1 | cut -d @ -f 2)";
echo "$s";
;;
2) case "$2" in # Second argument
g) #global| # Syntax to compare bash string to literal
s="$(npm list --depth=0 -g $1 | grep $1 | cut -d @ -f 2)";
echo "$s";
;;
l) #Latest
npm view $1 version; # 'npm info $1 version' does the same thing
;;
*) echo 'Invalid arguments';
;;
esac;
;;
*) echo 'Invalid arguments';
;;
esac;
}
export -f npmv
Now all I have to do is type:
- **npmv** for the version of npm, for example, `NPM version is: 4.2.0`
- **npmv <package-name>** for the local version, for example, `0.8.08`
- **npmv <package-name> g** for global version, for example, `0.8.09`
- **npmv <package-name> l** for latest version, for example, `0.8.10`
Note *-d* on the *cut* command means delimit by, followed by @, and then f means field. The '2' means the second field since there will be one either side of the *@* symbol.
[1]:
[To see links please register here]