Getting your npm package version from bash

This question came up for me and since I spent more than 5 minutes looking it up and didn’t find this answer anywhere else I wanted to document it.

The way to do it through npm is to add a script to your package.json file like so:

  "name": "example",
  "version": "1.0.0",
  "scripts": {
    "version": "echo $npm_package_version"

Then you can do this to get the version from bash:

$ CURRENT_VERSION=$(npm run version --silent)

If you want to do automation as npm scripts, you can just access the $npm_package_version variable directly in your scripts.

The longer explanation here is that when you run an npm script it will automatically pull out all of the values from the package.json and put them into environment variables with the pattern npm_package_*. This means you can expose those variables as scripts if you want to use them externally.

The alternate version that I saw elsewhere was to just grab it using node like so:

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

Author: justinmchase

I'm a Software Developer from Minnesota.

3 thoughts on “Getting your npm package version from bash”

    1. Yes, you should be able to using the same techniques.

        "scripts": {
          "version": "echo $npm_package_dependencies_fdih-testcafe-page-objects"


      CURRENT_VERSION=$(node -p "require('./package.json').dependencies.fdih-testcafe-page-objects")
  1. Thank you. Yup i tried that..but i am getting the following error.


    ReferenceError: testcafe is not defined
    at [eval]:1:45
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object. ([eval]-wrapper:6:22)
    at Module._compile (module.js:652:30)
    at evalScript (bootstrap_node.js:466:27)
    at startup (bootstrap_node.js:167:9)
    at bootstrap_node.js:612:3

Leave a Reply

%d bloggers like this: