I'd like to run yarn commands programmatically from node.js, but can't find any sdk or cli utility. The only thing is to spawn a new process, but that's hacky...
As of January 2019, Yarn does not have an API that you can call directly. You cannot require Yarn and use yarn commands similar to npm
var npm = require('npm');
npm.load(function(err) {
  // handle errors
  // install module ffi
  npm.commands.install(['ffi'], function(er, data) {
    // log errors or data
  });
You can only use node's child_process to execute the yarn command.
const { exec } = require('child_process');
exec('yarn add package@beta', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
  // the *entire* stdout and stderr (buffered)
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With