Where does npm install packages?

后端 未结 23 1331
梦毁少年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)
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:34

    Global libraries

    You can run npm list -g to see which global libraries are installed and where they're located. Use npm list -g | head -1 for truncated output showing just the path. If you want to display only main packages not its sub-packages which installs along with it - you can use - npm list --depth=0 which will show all packages and for getting only globally installed packages, just add -g i.e. npm list -g --depth=0.

    On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

    Windows XP - %USERPROFILE%\AppData\npm\node_modules
    Windows 7, 8 and 10 - %USERPROFILE%\AppData\Roaming\npm\node_modules

    Non-global libraries

    Non-global libraries are installed the node_modules sub folder in the folder you are currently in.

    You can run npm list to see the installed non-global libraries for your current location.

    When installing use -g option to install globally

    npm install -g pm2 - pm2 will be installed globally. It will then typically be found in /usr/local/lib/node_modules (Use npm root -g to check where.)

    npm install pm2 - pm2 will be installed locally. It will then typically be found in the local directory in /node_modules

    0 讨论(0)
  • 2020-11-22 06:34

    If you are looking for the executable that npm installed, maybe because you would like to put it in your PATH, you can simply do

    npm bin
    

    or

    npm bin -g
    
    0 讨论(0)
  • 2020-11-22 06:34

    Expanding upon other answers.

    npm list -g
    

    will show you the location of globally installed packages.

    If you want to output that list to a file that you can then easily search in your text editor:

    npm list -g > ~/Desktop/npmfiles.txt
    
    0 讨论(0)
  • 2020-11-22 06:36

    I was beginning to going mad searching the real configuration, so here is the all list of configuration files on linux:

    • /etc/npmrc
    • /home/youruser/.npmrc
    • /root/.npmrc

    on windows: - c/Program\ Files/nodejs/node_modules/npm/npmrc

    Then in the file the prefix is configured:

    prefix=/usr
    

    The prefix is defaulted to /usr in linux, to ${APPDATA}\npm in windows

    The node modules are under $prefix tree, and the path should contain $prefix/bin

    There may be a problem :

    • When you install globally, you use "sudo su" then the /root/.npmrc may be used!
    • When you use locally without sudo: for your user its the /home/youruser/.npmrc.
    • When your path doesn't represent your prefix
    • When you use npm set -g prefix /usr it sets the /etc/npmrc global, but doesn't override the local

    Here is all the informations that were missing to find what is configured where. Hope I have been exhaustive.

    0 讨论(0)
  • 2020-11-22 06:38

    In earlier versions of NPM modules were always placed in /usr/local/lib/node or wherever you specified the npm root within the .npmrc file. However, in NPM 1.0+ modules are installed in two places. You can have modules installed local to your application in /.node_modules or you can have them installed globally which will use the above.

    More information can be found at https://github.com/isaacs/npm/blob/master/doc/install.md

    0 讨论(0)
提交回复
热议问题