How to run `yarn tag` programmatically from node.js?

Viewed 9006

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...

1 Answers

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}`);
});
Related