Where does npm install packages?

后端 未结 23 1382
梦毁少年i
梦毁少年i 2020-11-22 06:03

Can someone tell me where can I find the Node.js modules, which I installed using npm?

23条回答
  •  太阳男子
    2020-11-22 06:33

    If you're trying to access your global dir from code, you can backtrack from process.execPath. For example, to find wsproxy, which is in {NODE_GLOBAL_DIR}/bin/wsproxy, you can just:

    path.join(path.dirname(process.execPath), 'wsproxy')
    

    There's also how the npm cli works @ ec9fcc1/lib/npm.js#L254 with:

    path.resolve(process.execPath, '..', '..')
    

    See also ec9fcc1/lib/install.js#L521:

    var globalPackage = path.resolve(npm.globalPrefix,
                                     'lib', 'node_modules', moduleName(pkg))
    

    Where globalPrefix has a default set in ec9fcc1/lib/config/defaults.js#L92-L105 of:

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

提交回复
热议问题