Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) })()")
like image 493
Sai Avatar asked Jun 29 '17 19:06

Sai


People also ask

What is version in package json?

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.

Is version required in package json?

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.

What is npm version command?

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.


Video Answer


2 Answers

If you have jq installed, it's really easy:

jq -r .version package.json
like image 134
Brett Beatty Avatar answered Oct 05 '22 18:10

Brett Beatty


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

like image 39
Sai Avatar answered Oct 05 '22 18:10

Sai