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 "micromatch@3.1.10"', 'info \r=> Found "fast-glob#micromatch@4.0.2"', '' ]
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
├─ camelcase@6.2.0
└─ yargs-parser@13.1.2
└─ camelcase@5.3.1