On my root directory of my project, I have a package.json. While doing the CI build, I am trying to capture the version properties from the file. Since I am running on a node container, the following command is possible.
node -p -e require('./package.json').version
I included the above in scripts property of package.json
"scripts": {
"version": "node -p -e require('./package.json').version"
},
I am capturing it in a variable using
export VERSION=$(npm run version)
which seems to capture a lot more than the result of the npm command. Build environment is a Nodejs10 container built on rhel7.
jq is not available and something without it may be better
Any suggestions?
"version": "node -p \"require('./package.json').version\""
- The-e
option is not necessary, also the js to be evaluated must be passed as a string. Note it's been encased in JSON escaped double quotes, i.e.\"
. 2) Also capture and export it using the following code instead;export VERSION=$(npm run version -s)
- note the additional-s
option (which is the shorthand for--silent
) - this ensures you capture only the version (e.g.1.0.0
) and not the additional npm log info.