Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract version from package.json (NPM) using bash / shell
# Version key/value should be on his own line
PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g')
echo $PACKAGE_VERSION
@jonathansamines

This comment has been minimized.

Copy link

commented Dec 16, 2015

Hey really impressive script, it also works to get any other property (top level at least) from the package.json file!

@swissmanu

This comment has been minimized.

Copy link

commented Jan 18, 2016

i had to trim some whitespaces, so here's my extended version if you need this too:

# Version key/value should be on his own line
PACKAGE_VERSION=$(cat package.json \
  | grep version \
  | head -1 \
  | awk -F: '{ print $2 }' \
  | sed 's/[",]//g'
  | tr -d '[[:space:]]')

echo $PACKAGE_VERSION
@dbaba

This comment has been minimized.

Copy link

commented Jan 24, 2016

@swissmanu it's helpful! but a backslash was missing after | sed 's/[",]//g'.

PACKAGE_VERSION=$(cat package.json \
  | grep version \
  | head -1 \
  | awk -F: '{ print $2 }' \
  | sed 's/[",]//g' \
  | tr -d '[[:space:]]')

echo $PACKAGE_VERSION
@FlorianWendelborn

This comment has been minimized.

Copy link

commented Feb 11, 2016

In case anybody else wants to auto-git-tag the npm version when publishing, here you go:

{
    "scripts": {
        "postpublish" : "PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag $PACKAGE_VERSION && git push --tags"
    }
}
@jimlyndon

This comment has been minimized.

Copy link

commented Feb 17, 2016

To add to @dodekeract's purpose, you really should use npm version patch to bump the semver and get back the version (e.g., vX.X.X), which can be trimmed with cut like so:

VERSION=$(npm version patch)
VERSION=$(echo $VERSION | cut -c 2-)

Then you can run npm pack and use $VERSION to name and publish the zipped package, if need be.

@FlorianWendelborn

This comment has been minimized.

Copy link

commented Feb 26, 2016

@jimlyndon Unfortunately NPM still resets package.json formatting, which is why I personally won't use it to do anything with my package.jsons. I'd be happy to change my postpublish script to a script which uses NPM to find out the version though, as long as it doesn't have to write to my package.jsons.

@todvora

This comment has been minimized.

Copy link

commented Mar 11, 2016

Where node binary is available:
PACKAGE_VERSION=$(node -p -e "require('./package.json').version")

@plouc

This comment has been minimized.

Copy link

commented Mar 25, 2016

Another approach should be to use package.json variables.

It requires that npm is available and you add an extra script to your package.json.

{
  "name": "sample-package",
  "version": "1.0.0",
  "scripts": {
    "vars": "env | grep npm_package_"
  }
}

then

npm --loglevel silent run vars

sample output

npm_package_name=sample-package
npm_package_version=1.0.0
…

so @dodekeract, you could just use $npm_package_name, doesn't require the extra vars script.

@boldfacedesign

This comment has been minimized.

Copy link

commented May 10, 2016

PACKAGE_VERSION=$(grep -m1 version package.json | awk -F: '{ print $2 }' | sed 's/[", ]//g')

or if you have jq:
PACKAGE_VERSION=$(jq -r ".name" package.json)

@bagrounds

This comment has been minimized.

Copy link

commented Sep 12, 2016

PACKAGE_VERSION=$(sed -nE 's/^\s*"version": "(.*?)",$/\1/p' package.json)
echo $PACKAGE_VERSION
@oshalygin

This comment has been minimized.

Copy link

commented Nov 28, 2016

cleanest solution is @bagrounds

@petergi

This comment has been minimized.

Copy link

commented Dec 9, 2016

@boldfacedesign. Um... that's not the package version... (jq ...)

@strugee

This comment has been minimized.

Copy link

commented Dec 13, 2016

Nice script! Couple of comments:

  1. Not sure why the head -1 is in there - presumably in case there's a dependency that contains the name version, but in that case the head -1 isn't good enough anyway since dependencies isn't guaranteed to be after version. Not sure there's an elegant way to solve this. Tightening up the regex to look for "version": would help but doesn't completely solve the bug.
  2. This is a useless use of cat. Use grep 'version' package.json (or grep '"version":' package.json) instead

Also @piouc npm actually recognizes a special env script such that if you run npm run env it'll run the system binary env. Hence you can do e.g. npm run env | grep npm_package_ without any package.json changes. See npm-run-script(1)

@NathanielInman

This comment has been minimized.

Copy link

commented Jan 5, 2017

No need for sed or grep here. I solved the issue in a deploy script with

awk '/version/{gsub(/("|",)/,"",$2);print $2};' package.json

here's a stupid but fun way w/o awk or sed

grep version package.json | cut -c 15- | rev | cut -c 3- | rev

Cheers!

@jakwuh

This comment has been minimized.

Copy link

commented Jan 14, 2017

Hi! I see this is all about npm so we do have node installed. Thus, I see the simplest solution is:

node -pe "require('./package.json').version"
@ChristianUlbrich

This comment has been minimized.

Copy link

commented Jan 27, 2017

@jakwuh THANKS! This one is really portable and will be future-proof!

@kevinnguy

This comment has been minimized.

Copy link

commented Jan 30, 2017

@jakwuh v nice!

@ChristianUlbrich

This comment has been minimized.

Copy link

commented Feb 7, 2017

And for all the ev0l shell hackers, you can cheat by using jq and having it installed it is a mere: cat package.json | jq '.version'!

@delfrrr

This comment has been minimized.

Copy link

commented Feb 22, 2017

why not use $ npm show ./ name ?

@unixben

This comment has been minimized.

Copy link

commented Mar 16, 2017

@delfrrr because that will give you a potentially invalid result.

Consider a local application called, 'express'.

If you run $ npm show ./ name, you get what you would expect, ie 'express'. If the package.json has version 9.9.9 and you run $ npm show ./ version, what would be the expected result? 9.9.9?

No. It would be whatever the published version of the npm package called express is at the time.

Why does this happen? Because in this case ./ refers to the current working directory, 'express'.

@loretoparisi

This comment has been minimized.

Copy link

commented Mar 29, 2017

Not all solutions presented here will work on macOS, but those two:

$ awk '/version/{gsub(/("|",)/,"",$2);print $2};' package.json
1.1.7-20170329
$ grep version package.json | cut -c 15- | rev | cut -c 3- | rev
1.1.7-20170329

Supposed that you are using version with - i.e. build number you can do

awk '/version/{gsub(/("|",)/,"",$2);print $2};' package.json | perl -n -e'/(.+)-(.+)/ && print $1'
1.1.7
@4F2E4A2E

This comment has been minimized.

Copy link

commented Apr 14, 2017

# Gitlab CI working version
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F= "{ print $2 }" | sed 's/[version:,\",]//g' | tr -d '[[:space:]]')
@nickleefly

This comment has been minimized.

Copy link

commented Apr 24, 2017

npm i -g json
json dependencies -a < package.json | grep : | sed 's/^ *//;s/"//g;s/: /@/;s/,$//'
json dependencies -a < package.json | grep : | sed 's/[",]//g;s/: /@/;s/,$//'
@arzyu

This comment has been minimized.

Copy link

commented May 10, 2017

Where node binary is available:
PACKAGE_VERSION=$(node -p -e "require('./package.json').version")

@todvora, thanks, It's the simplest way I've ever seen. Also works without -e option.

PACKAGE_VERSION=$(node -p "require('./package.json').version")
@peekg

This comment has been minimized.

Copy link

commented May 25, 2017

grep '"version":' package.json | cut -d\" -f4

@GuyMograbi

This comment has been minimized.

Copy link

commented Jul 19, 2017

FYI - if you can run your script from within npm scripts, you can simply export $npm_package_version to some variable and use that.

cd PROJECT_ROOT_DIR
VERSION=`npm run echo-version`

Where echo-version is simply echo $npm_package_version

@yowainwright

This comment has been minimized.

Copy link

commented Sep 5, 2017

I like @GuyMograbi approach = THANKS!

In package.json:

"version": "0.0.1"
"scripts": {
     "version": "echo $npm_package_version"
}

In a terminal:

npm run version
@SushilShrestha

This comment has been minimized.

Copy link

commented Oct 30, 2017

Thanks @GuyMograbi You are the best!! 🎉 🎉 🎉

@ORESoftware

This comment has been minimized.

Copy link

commented Nov 11, 2017

There is an easier way to get a property from a json string. Using a package.json file as an example, try this:

#!/usr/bin/env bash
str=`cat package.json`;
my_val="$(node -pe "JSON.parse(\`$str\`)['version']")"

or

#!/usr/bin/env bash
prop="version"
my_val="$(node -pe "require('./package.json')['$prop']")"

both techniques work great. enjoy.

read more here: https://stackoverflow.com/questions/47234740/read-name-property-of-package-json-file-with-bash/47234821

@luismr

This comment has been minimized.

Copy link

commented Feb 6, 2018

node -p "require('./package.json').version"
@synthecypher

This comment has been minimized.

Copy link

commented Feb 14, 2018

FYI you need to have sed replace white space as well.

# Version key/value should be on his own line
PACKAGE_VERSION=$(cat package.json \
  | grep version \
  | head -1 \
  | awk -F: '{ print $2 }' \
  | sed 's/[",\t ]//g')

echo $PACKAGE_VERSION
@igordeoliveirasa

This comment has been minimized.

Copy link

commented Feb 20, 2018

node -p "require('./package.json').version" is the best!

@shardyMBAI

This comment has been minimized.

Copy link

commented Feb 22, 2018

https://stedolan.github.io/jq/

echo -e $(jq ".engines.node" package.json)

@phdesign

This comment has been minimized.

Copy link

commented Apr 20, 2018

Here's a script I used to pull major.minor from the package.json, append with the revision from the latest git tag and increment, then create new git tag and save to packge.json.

# Create a git tag of the new version to use
# If package.json major and minor versions match last tag, then increment last tag. Else use package.json major.minor.0.
"{ sed -nE 's/^[ \\t]*\"version\": \"([0-9]{1,}\\.[0-9]{1,}\\.)[0-9x]{1,}\",$/\\1/p' package.json; git describe --abbrev=0 | sed -E 's/^v([0-9]{1,}\\.[0-9]{1,}\\.)([0-9]{1,})$/\\1 \\2/g'; } | tr \"\\n\" \" \" | awk '{printf($1==$2?\"v\"$2$3+1:\"v\"$1\"0\")}' | xargs -I {} git tag -a {} -m \"{}\"\n"
# Update package.json based on the git tag we just created
npm --no-git-tag-version version from-git

Long explanation here: http://phdesign.com.au/programming/auto-increment-project-version-from-travis/

@justinmchase

This comment has been minimized.

Copy link

commented May 3, 2018

Here is the solution using the latest version of npm and their fancy new npx command:

npx -c 'echo "$npm_package_version"'
@gonzalesraul

This comment has been minimized.

Copy link

commented Jun 21, 2018

pure shellscript in a single command

sed -n 's/.*"version": "\(.*\)",/\1/p package.json' #gets the version
@mtavkhelidze

This comment has been minimized.

Copy link

commented Jul 12, 2018

jq -r ".version" < package.json

@Jeff-Tian

This comment has been minimized.

Copy link

commented Oct 18, 2018

node -p "require('./package.json').version"

works like a charm!

@js2me

This comment has been minimized.

Copy link

commented Dec 21, 2018

node -p "require('./package.json').version"

Looks simple and works fine! Thanks you!

@mvertes

This comment has been minimized.

Copy link

commented Dec 26, 2018

Using just awk with " as a separator:
awk -F\" '/"version":/ {print $4}' package.json

@zetareticoli

This comment has been minimized.

Copy link

commented Feb 28, 2019

Is it possible to print in HTML?

@cmacdonnacha

This comment has been minimized.

Copy link

commented Mar 13, 2019

Is there anyway to extract 2 variables? Like version and name?

@ziyoung

This comment has been minimized.

Copy link

commented Mar 22, 2019

In case anybody else wants to auto-git-tag the npm version when publishing, here you go:

{
    "scripts": {
        "postpublish" : "PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag $PACKAGE_VERSION && git push --tags"
    }
}

head -1 may be redundant if use grep \"version\".

@lucca65

This comment has been minimized.

Copy link

commented Mar 25, 2019

Made it work with less effort:

grep 'version' package.json | cut -d '"' -f4

Or, if you want it without any spaces:

grep 'version' package.json | cut -d '"' -f4 | tr -d '[[:space:]]'
@piuccio

This comment has been minimized.

Copy link

commented Apr 3, 2019

Here is the solution using the latest version of npm and their fancy new npx command:

npx -c 'echo "$npm_package_version"'

This is the solution that worked for me, I wanted to have the package.json version inside a shell variable, so

CURRENT_VERSION="$(npx -c 'echo "$npm_package_version"')"
@ravshansbox

This comment has been minimized.

Copy link

commented May 9, 2019

just npm view . version.

@nidkil

This comment has been minimized.

Copy link

commented May 15, 2019

Another one for fun and laughs:

grep '"version"' package.json | cut -d '"' -f 4
@nidkil

This comment has been minimized.

Copy link

commented May 15, 2019

npm view . version

Only works if it is a published in the npm registry.

@alavaros

This comment has been minimized.

Copy link

commented May 16, 2019

Another approach should be to use package.json variables.

It requires that npm is available and you add an extra script to your package.json.

{
  "name": "sample-package",
  "version": "1.0.0",
  "scripts": {
    "vars": "env | grep npm_package_"
  }
}

then

npm --loglevel silent run vars

sample output

npm_package_name=sample-package
npm_package_version=1.0.0
…

so @dodekeract, you could just use $npm_package_name, doesn't require the extra vars script.

I'm trying to use the package.json variables, but it shows an empty string:

npm --loglevel silent run vars

Output:

...
npm_package_scripts_vars=env | grep npm_package_
npm_package_scripts_version=echo $npm_package_version
npm_package_version=0.1.0

But when I run:

echo $npm_package_version

It doesn't show anything.

What am I missing? To know, I'm working on Windos 10 and using Cmder as a command line tool and bash.

@erhhung

This comment has been minimized.

Copy link

commented Aug 1, 2019

Here's a Perl one-liner:

perl -ne 'print "$1\n" if /"version":\s*"(.*?)"/' package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.