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) })()")
The syntax is in JSON format where the key is the name of the package and the value is the version of the package to be used. npm uses the package. json file to specify the version of a package that your app depends on. The version number is in semver syntax which designates each section with different meaning.
Required name and version fieldsA package. json file must contain "name" and "version" fields. The "name" field contains your package's name, and must be lowercase and one word, and may contain hyphens and underscores.
Synopsis. npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git] 'npm [-v | --version]' to print npm version. 'npm view <pkg> version' to view a package's published version. 'npm ls' to inspect current package/dependency versions.
If you have jq installed, it's really easy:
jq -r .version package.json
So i went ahead and used the readJson function from http://dailyraisin.com/read-json-value-in-bash/ which perfectly fits my need here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With