How to know the version of currently installed package from yarn.lock
I found out that yarn why
is the best way to find out the currently installed version of a package (Thanks to one of my colleague who point out to me). This is how my test code looks in the JavaScript.
const { spawnSync } = require('child_process');
const packageName = 'micromatch';
const whyBuffer = spawnSync('yarn', ['why', packageName]);
const grepBuffer = spawnSync('grep', ['Found'], { input: whyBuffer.stdout });
const outputArray = grepBuffer.stdout.toString().split('\n');
console.log(outputArray); // ['info \r=> Found "[email protected]"', 'info \r=> Found "fast-glob#[email protected]"', '' ]
const parsedOutputArray = outputArray.filter(output => output.length > 0).map((output) => output.split('@')[1].replace('"', ''))
console.log(parsedOutputArray); // [ '3.1.10', '4.0.2' ]
Since, you know the name of the package, do this:
yarn list --pattern <package_name>
The above command will get you all installed versions of a package at any depth. For example, I have different versions of camelcase
library installed at various depths. On running the command : yarn list --pattern "camelcase"
, this is the output:
yarn list v1.22.5
├─ [email protected]
└─ [email protected]
└─ [email protected]