Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get current package's version using only npm

Tags:

node.js

npm

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.

like image 877
Matheus208 Avatar asked Dec 05 '18 17:12

Matheus208


People also ask

How can we check the current version of npm?

To see the most current version of a package in the npm repository, use the npm view npm get version of package-name version command.

How do I change npm to specific version?

If you want to downgrade npm to a specific version, you can use the following command: npm install -g npm@[version. number] where the number can be like 4.9. 1 or 8 or v6.

How do I check my react package version?

The other method is also straightforward, you can check the React app version by heading over to node_modules/react/cjs/react. development. js. You can see the react project version in the commented section as showing given below.


2 Answers

Using npm:

npm -s run env echo '$npm_package_version'

from npm v7.2:

npm pkg get version

like image 190
Mbrevda Avatar answered Oct 22 '22 20:10

Mbrevda


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

like image 20
Kaz Avatar answered Oct 22 '22 19:10

Kaz