Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 677
Totty.js Avatar asked Oct 28 '25 17:10

Totty.js


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}`);
});
like image 172
Hanxue Avatar answered Oct 30 '25 06:10

Hanxue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!