Search code examples
npmyarnpkg

How do I upgrade all scoped packages with Yarn?


Is it possible to upgrade all specific scoped packages in the dependencies section of my package.json by using the Yarn package manager?

For example:

yarn upgrade @scope/* 

This will upgrade all scoped packages in yarn.lock and package.json file.


Solution

  • Since there's no way to do this currently with Yarn, I've written a very short Node script to do what you want:

    var fs = require('fs');
    var child_process = require('child_process');
    
    var filterRegex = /@angular\/.*/;
    
    fs.readFile('./package.json', function(err, data) {
        if (err) throw err;
    
        var dependencies = JSON.parse(data)['dependencies'];
        Object.keys(dependencies).forEach(function(dependency) {
            if (filterRegex.test(dependency)) {
                console.log('Upgrading ' + dependency);
                child_process.execSync('yarn upgrade ' + dependency);
            } else {
                console.log('Skipping ' + dependency);
            }
        });
    });
    

    Here's a quick explanation of what that does:

    • it loads the package.json from the directory that the terminal is currently in

    • we then parse the JSON of the package.json and get the "dependencies" key

    • for each dependency, we run the regex specified as the filterRegex (if you need to change this or want an explanation of the regex syntax, I would test with RegExr. I used @angular as an example prefix for the regex)

    • if the dependency matches, we run yarn upgrade [DEPENDENCY] and log it

    • otherwise, we log that it was skipped.

    Let me know if you have any trouble with this, but it should solve your issue until the Yarn team come up with something better.