0

I need to access the list of globally installed packages (those installed via npm install -g) so I need to obtain the global prefix.

I already know about npm config get prefix (see How to get the npm global path prefix) But, how can I code it on my node.js program?

Community
  • 1
  • 1
PA.
  • 28,486
  • 9
  • 71
  • 95

2 Answers2

1

You can use child_process.exec() to run the command:

var child_process = require('child_process');
child_process.exec('npm config get prefix', function(err, stdout){
    var prefix = stdout.toString(); // stdout was a Buffer
    console.log(prefix);
});
Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

I believe you can use npm programatically, as such:

var npm = require('npm')
var prefix = npm.config.get('prefix');

More information here:

jportela
  • 61
  • 5