node.jsshellunixdockerpackage.json

Extract package.json version using shell script


I'm trying to extract the value of the version field in package.json from the build.sh file. Is there a way to do this? Looks like i can use the below to do the extraction, but what if node is not available globally where i execute the build.sh file so looking for a generic way to exact the value from package.json into build.sh file.

VERSION=$(node -e "(function () { console.log(require('./package.json').version) })()")

Solution

  • So i went ahead and used the readJson function from http://dailyraisin.com/read-json-value-in-bash/ which perfectly fits my need here