Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use yarn/npm from node script

Inside my script I want to use some functionality from yarn/npm

Like get results of yarn info packageName. I could spawn command and get output, but I believe there should be more correct way to do that.

Can't find any documentation about usage of yarn as package. Any ideas?

like image 353
Vasiliy Vanchuk Avatar asked Oct 28 '25 14:10

Vasiliy Vanchuk


1 Answers

NPM and Yarn were designed as CLI tools. It's possible to load them with require but since executable script does a lot of work on options processing, this isn't practical.

The most straightforward and predictable way is to run them with child_process spawn or exec. There are npm-programmatic and yarn-programmatic third-party packages that do this internally. Since their APIs don't necessarily cover 100% of CLI functionality, it may still be beneficial to run commands directly.

There are open issues for NPM and Yarn regarding programmatic use.

like image 185
Estus Flask Avatar answered Oct 31 '25 03:10

Estus Flask



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!