7

I want to programmatically find out the folder where npm installs global modules. This question is similar, but the answer doesn't work for globally installed modules: How to get details of npm installed modules programatically?

Community
  • 1
  • 1
B T
  • 57,525
  • 34
  • 189
  • 207

3 Answers3

4

NPM's primary job is "to put things on your computer." Their folders documentation details what it puts where

Due to differences between operating systems, the .npmrc config, and the prefix property, it's easiest to rely on npm to determine the global install directory by using npm root like this:

$ npm root -g

You can execute a command line binary with Node.js like this:

const { exec } = require('child_process')
const { promisify } = require('util');

async function main() {
    let execAsync = promisify(exec);
    let { stdout: globalPath } = await execAsync("npm root -g");
    console.log(globalPath);
}

Alternatively, in order to access the npm module programmatically, you'll need to install it locally:

$ npm i npm --save

And then you can run the following code:

const npm = require("npm")
const { promisify } = require('util');

async function main() {
    await promisify(npm.load)()
    let globalPath = npm.root
    console.log(globalPath)
}

Further Reading

KyleMit
  • 30,350
  • 66
  • 462
  • 664
3

From the nodejs website:

globally - This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied.

To get the prefix enter:

npm config get prefix

Edit:

To do it from node use the npm npm module. Something like this will work:

var npm = require("npm")
var myConfigObject = {}
npm.load(myConfigObject, function (er) {
    if (er) return handleError(er)
    console.log(npm.get('prefix'));
})
Doug Amos
  • 4,243
  • 1
  • 22
  • 23
  • Thank you sir! Now if I could only figure out how load the global npm module so I could get the correct prefix from javascript rather than from a command execution, I'd be all set! – B T Jun 21 '13 at 07:41
  • So actually tho, mine was in /node_modules - there is no lib or bin folder for me. I'm on windows. – B T Jun 21 '13 at 07:43
  • There's an npm module for npm. Maybe this could help. – Doug Amos Jun 21 '13 at 07:50
  • Tried it. Unfortuantely A) the version you wrote up there won't work (it should be `npm.prefix`), and B) the prefix it returns is the prefix of the local module installation, not the global installation – B T Jun 21 '13 at 17:32
  • 1
    Another note, it looks like they did it slightly differently on windows than linux. The global modules are installed at /node_modules in widows, whereas they're installed at /lib/node_modules in linux. – B T Jul 14 '13 at 22:05
  • 1
    module.paths.push(/node_modules) – airportyh Jan 26 '14 at 02:05
1

If you don't want to depend on npm:

const { resolve } = require('path')    
const globalPath = resolve(process.execPath, '../../lib/node_modules')
justmoon
  • 1,311
  • 1
  • 10
  • 9