Extract package.json version using shell script

Viewed 12430

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) })()")
6 Answers

If you really want to use shell script then simply try it

sed 's/.*"version": "\(.*\)".*/\1/;t;d' ./package.json

Credit to metakermit

NODE_VERSION=$(node -p -e "require('./package.json').version")
echo $NODE_VERSION
Related