0

I have quite an unusual question.

I don't mean make

script/test.js

, and then in package

"node scripts/test.js"

Instead, I want to somehow run function npm list from my test.js file

As in

let list = npm list

, and i have no idea how to do it, is it even possible? And if so, how? I would also like to save it as string or object. any can work

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    Possible duplicate of [Execute a command line binary with Node.js](https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js) – Ivan Bacher Jul 26 '18 at 09:36

1 Answers1

1

Yes, you can do this, here is the code:

var execSync = require('child_process').execSync;

var list = execSync('yarn list', { encoding: 'utf-8' });
console.log(list);
Viktor Vlasenko
  • 2,332
  • 14
  • 16