2 * global-prefix <https://github.com/jonschlinkert/global-prefix>
4 * Copyright (c) 2015 Jon Schlinkert.
5 * Licensed under the MIT license.
10 var homedir = require('homedir-polyfill');
11 var path = require('path');
12 var ini = require('ini');
13 var fs = require('fs')
17 if (process.env.PREFIX) {
18 prefix = process.env.PREFIX;
20 // Start by checking if the global prefix is set by the user
23 // homedir() returns undefined if $HOME not set; path.resolve requires strings
24 var userConfig = path.resolve(home, '.npmrc');
25 prefix = readPrefix(userConfig);
29 // Otherwise find the path of npm
32 // Check the built-in npm config file
33 var builtinConfig = path.resolve(npm, '..', '..', 'npmrc');
34 prefix = readPrefix(builtinConfig);
37 // Now the global npm config can also be checked.
38 var globalConfig = path.resolve(prefix, 'etc', 'npmrc');
39 prefix = readPrefix(globalConfig) || prefix;
43 if (!prefix) fallback();
48 var isWindows = require('is-windows');
50 // c:\node\node.exe --> prefix=c:\node\
51 prefix = process.env.APPDATA
52 ? path.join(process.env.APPDATA, 'npm')
53 : path.dirname(process.execPath);
55 // /usr/local/bin/node --> prefix=/usr/local
56 prefix = path.dirname(path.dirname(process.execPath));
58 // destdir only is respected on Unix
59 if (process.env.DESTDIR) {
60 prefix = path.join(process.env.DESTDIR, prefix);
67 return fs.realpathSync(require('which').sync('npm'));
73 function readPrefix(configPath) {
75 var data = fs.readFileSync(configPath, 'utf-8');
76 var config = ini.parse(data);
77 if (config.prefix) return config.prefix;
84 module.exports = prefix;