PA.
PA.

Reputation: 29369

cross-platform programming technique to obtain the global npm prefix

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?

Upvotes: 0

Views: 71

Answers (2)

jportela
jportela

Reputation: 61

I believe you can use npm programatically, as such:

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

More information here:

Upvotes: 0

Scimonster
Scimonster

Reputation: 33409

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

Upvotes: 1

Related Questions