The following script searches the package.json file in the directory where it is executed for the specified package and prints out its version:
# getPackageVersion.sh
# Searches the package.json for a specific package and prints out its version
#
# Usage:
# chmod +x getPackageVersion.sh
# ./getPackageVersion.sh <package_name>
packageName=$1
if [ ! -f ./package.json ]; then
echo "package.json file not found in current directory."
exit
fi
if [ -z "$packageName" ]
then
echo "No package name given."
exit
fi
packageJsonOutput=$(cat package.json | grep "\"$packageName\": \"*\"")
version=(${packageJsonOutput//:/ })
version=${version[1]//\"/}
if [ "$version" == "" ]
then
echo "Package not found."
exit
fi
echo "$version"
Usage
chmod +x getPackageVersion.sh
./getPackageVersion.sh <package_name>
Example
$ cd ~/sw/your-project/
$ ./getPackageVersion.sh debug
4.3.1