17

How do I get the current version from package.json using npm?

I know that npm version will output my package's version along with npm and other dependencies. But I need a short command to get only my package version to use on a CI. So ideally, no extra input as the CI doesn't know what project it's dealing with.

npm version from-git --allow-same-version would also work, if it didn't try to tag the new version on Git.

Matheus208
  • 1,289
  • 2
  • 13
  • 26
  • 1
    You'll need to utilize either an npm script or nodejs script as _npm_ does not provide a direct command to achieve this. See my answer [here](https://stackoverflow.com/questions/48609931/how-can-i-reference-package-version-in-npm-script#answer-48619640) which indicates how to reference npm's built-in variable named `npm_package_version` to retrieve the current version from _package.json_. – RobC Dec 06 '18 at 11:01
  • no accepted answer? – ricoms Mar 02 '23 at 16:50

5 Answers5

26

Using npm:

npm -s run env echo '$npm_package_version'

from npm v7.2:

npm pkg get version

Mbrevda
  • 2,888
  • 2
  • 27
  • 35
  • These commands also make it really easy to get other package properties, such as `name` with `npm -s run env echo '$npm_package_name'` or `npm pkg get name`. Note that the `npm pkg get` command return values include surrounding quotes. – derpedy-doo Oct 17 '21 at 20:22
  • Both commands are not working for me. – Mac Ignacio May 03 '22 at 06:38
  • It could also be used as shebang in external scripts (they may even resides in another fs location): `#!/usr/bin/env -S npm -s run env bash` (and you can replace bash with python, node, ...). – ThoSil May 31 '22 at 12:57
19

There is no direct npm command to show only your package version, but you can use this hack from your project folder:

node -e "console.log(require('./package.json').version);"

If you want to use an npm command you can wrap it into a "script" in your package.json file :

"scripts": {
    "version": "node -e console.log(require('./package.json').version);"
}

Now you can launch npm run version to get your package version

phoenix
  • 7,988
  • 6
  • 39
  • 45
Kaz
  • 718
  • 1
  • 7
  • 20
8

To build on Kaz's answer this is slightly shorter.

PACKAGE_VERSION=`node -p "require('./package.json').version"`
Ben Winding
  • 10,208
  • 4
  • 80
  • 67
1

If using grep and sed is not a problem for you, this is working pretty good for me:

VERSION_NUMBER=$(npm version | grep @ | sed -re "s/\{ '.*': '(.*)',?/\1/g")

Explanations:

  • npm version gives you all the information
  • grep @ catches the line where you have the version
  • sed -re "s/\{ '.*': '(.*)',?/\1/g" replaces the text by just the version number
Michael Arbib
  • 103
  • 2
  • 10
0

If your project is backed by git and you are letting npm version tag your builds, you can probably leverage git describe:

echo The current build is \"`git describe`\". 
==> The current build is "v0.0.2".

It's not technically using npm to get the version, but it's what I ended up using after trying to find a 'real' npm based solution and finding this SO that makes it quite clear that there isn't a nice clean command available.

ggranum
  • 999
  • 8
  • 10