如果您尝试从代码访问全局目录,则可以从进行回溯process.execPath
。例如,要查找wsproxy
位于中的{NODE_GLOBAL_DIR}/bin/wsproxy
,您可以:
path.join(path.dirname(process.execPath), 'wsproxy')
还有如何npm
CLI作品@ ec9fcc1
/lib/npm.js#L254有:
path.resolve(process.execPath, '..', '..')
另请参阅ec9fcc1
/lib/install.js#L521:
var globalPackage = path.resolve(npm.globalPrefix,
'lib', 'node_modules', moduleName(pkg))
凡globalPrefix
在一组默认ec9fcc1
/lib/config/defaults.js#L92-L105的:
if (process.env.PREFIX) {
globalPrefix = process.env.PREFIX
} else if (process.platform === 'win32') {
// c:\node\node.exe --> prefix=c:\node\
globalPrefix = path.dirname(process.execPath)
} else {
// /usr/local/bin/node --> prefix=/usr/local
globalPrefix = path.dirname(path.dirname(process.execPath))
// destdir only is respected on Unix
if (process.env.DESTDIR) {
globalPrefix = path.join(process.env.DESTDIR, globalPrefix)
}
}
$HOME/.npm-global/lib/node_modules