Get package.json version from CLI

Using Node

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

The -p argument prints the result and the -e argument expects executable JS to follow it. This one is also helpful for getting specific properties from any JSON file.

Using jq

cat ./package.json | jq '.version'

Use the cat program to read the contents of the file and pipe it to the jq program to parse the JSON.

Links

Shell commands with Node

Running CLI Node scripts, capture the return value of a shell command:

const execSync = require('child_process').execSync;
const nodeVer = execSync('node -v', { encoding: 'utf8' });

Shell command reports in the CLI:

const execSync = require('child_process').execSync;
execSync('npm -g ls', { stdio: 'inherit' });