Where does npm install packages?
Asked Answered
E

23

1507

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

Edentate answered 8/5, 2011 at 9:39 Comment(4)
on linux mint it's $HOME/.npm-global/lib/node_modulesTennietenniel
Just so every one knows, installing without -g option will install a module to you working directory e.g. if you make a directory say ~/Desktop/tmp then cd ~/Desktop/tmp then do npm install appium then do ls you will see node_modules package-lock.json because you have installed a node module appium to your working directory... super confusing because -g should essentially be the default but is not.Shopper
npm config get prefix (https://mcmap.net/q/46147/-fixing-npm-path-in-windows-8-and-10)Unimproved
When using nvm to manage multiple versions of node, it's under $HOME/.nvm/versions/node/v15.9.0/lib where the directory will change depending on what version of node you are using. See github.com/nvm-sh/nvm/blob/master/README.md for more info on nvm. I found this directory with npm list -g | head -1 as stated in the selected answer.Gennie
J
1622

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

Johnsten answered 8/5, 2011 at 9:47 Comment(12)
If you're using nvm, then your global modules may be in one of several places depending on the version of node you're using at the time. The best way is to use npm list -g as suggested in another answer.Zolly
does not work for me, shows only bin folder. "npm root -g" does work.Retorsion
under Arch linux, the global modules are under /usr/lib. "npm --help" shows the exact location as last line of the output, e.g.: [email protected] /usr/lib/node_modules/npmSpurry
This is incorrect for non-global modules. Non-global modules are not installed in ./node_modules, but into an "appropriate" node_modules directory in the current directory or a parent directory, based on the presence of "node_modules" or "package.json". The upshot is that this can wind up being several directories above the current directory. See "More information" under docs.npmjs.com/files/folders.Earphone
and maybe placed in /usr/lib/node_modulesChanson
for windows 10,placed in %USERPROFILE%\AppData\Roaming\npm\node_modulesDiverse
"npm root -g" is correct - list might work but is far too much info, where as the other command just gives you the exact location you are looking for. https://mcmap.net/q/45215/-where-does-npm-install-packagesInstructive
npm root alone is pointless. If there's a node_modules in your working directory it will show it -- just like ls. Always use npm root -gKnee
At least on Windows, the location where packages are installed is not the same as the "bin" directory. So the directory to add to your path would be %USERPROFILE%\AppData\Roaming\npmCointon
Both for XP and Vista, 7, 8, 10 %APPDATA%\npm\node_modules points to the correct folder. %APPDATA% equals C:\Documents and Settings\{username}\Application Data in XP and C:\Users\{username}\AppData\Roaming from Vista to 10.Pharaoh
I found it on /usr/lib/ (there's a node_modules in it).Ventage
Regarding "Non-global libraries are installed the node_modules sub folder in the folder you are currently in." ... to my observation, this is not correct. npm install somepackage will use the same directory as the one returned by executing npm root. The logic seems to be: "from the current working directory (./), if it doesn't have a directory ./node_modules/, iteratively go up the parent directory chain until a directory node_modules/ is found. If none is found after checking the root directory /, then choose ./node_modules/ in the current working directory.Arand
K
735

The command npm root will tell you the effective installation directory of your npm packages.

If your current working directory is a node package or a sub-directory of a node package, npm root will tell you the local installation directory. npm root -g will show the global installation root regardless of current working directory.

Example:

$ npm root -g
/usr/local/lib/node_modules

See the documentation.

Knew answered 18/6, 2014 at 21:31 Comment(2)
Follow up question: does npm install -g do anything besides move these files to that location (I am using Red Hat if that matters), such as run a chmod command? I am trying to make a docker container that uses these modules, but does not use npm install (long story)Denitadenitrate
Very helpful if you use nvm as you may have multiple locations per node version, e.g. ~/.nvm/versions/node/v14.20.0/lib/node_modulesSolleret
M
116

For globally-installed modules:

The other answers give you platform-specific responses, but a generic one is this:

When you install global module with npm install -g something, npm looks up a config variable prefix to know where to install the module.

You can get that value by running npm config get prefix

To display all the global modules available in that folder use npm ls -g --depth 0 (depth 0 to not display their dependencies).

If you want to change the global modules path, use npm config edit and put prefix = /my/npm/global/modules/prefix in the file or use npm config set prefix /my/npm/global/modules/prefix.

When you use some tools like nodist, they change the platform-default installation path of global npm modules.

Maurene answered 4/12, 2014 at 15:50 Comment(1)
You answered my follow-up question: "Can someone tell me how I can change the location of the Node.js modules?" Note: The reason why this was important to me was so that I didn't have to change my PATH yet again. I uncommented the default suggestion in the npmrc file that {npm config get prefx} opens.Koran
H
72

On windows I used npm list -g to find it out. By default my (global) packages were being installed to C:\Users\[Username]\AppData\Roaming\npm.

Helmuth answered 25/3, 2014 at 10:59 Comment(1)
Just FYI - This is the executing user's %appdata%\npmRobena
N
29

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
Nenitanenney answered 2/1, 2018 at 21:43 Comment(3)
If you've globally installed npm packages where node was installed with homebrew and then npm updated with npm itself, you may be getting command not found errors. If so, add the aboveto your PATH: export PATH=$PATH:$(npm bin -g) voilaActinometer
npm bin has been removed since v9.0.0Corody
Any replacement?Metaphrast
M
24

If a module was installed with the global (-g) flag, you can get the parent location by running:

npm get prefix

or

npm ls -g --depth=0

which will print the location along with the list of installed modules.

Margaritamargarite answered 24/8, 2016 at 14:19 Comment(0)
W
22

Not direct answer but may help ....

The npm also has a cache folder, which can be found by running npm config get cache (%AppData%/npm-cache on Windows).

The npm modules are first downloaded here and then copied to npm global folder (%AppData%/Roaming/npm on Windows) or project specific folder (your-project/node_modules).

So if you want to track npm packages, and some how, the list of all downloaded npm packages (if the npm cache is not cleaned) have a look at this folder. The folder structure is as {cache}/{name}/{version}

This may help also https://docs.npmjs.com/cli/cache

Wasserman answered 20/5, 2017 at 9:45 Comment(0)
B
19

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

Bullough answered 8/5, 2011 at 16:30 Comment(3)
Something to note- with 1.0 modules are stored in /usr/local/lib/node_modules.Bullough
@EricSmith your link is broken tooFeminine
Link in answer is dead - "404 | Page not found".Hydrant
M
19

To get a compact list without dependencies simply use

npm list -g --depth 0
Mediaeval answered 14/3, 2019 at 18:0 Comment(0)
S
18

As the other answers say, the best way is to do

npm list -g

However, if you have a large number of npm packages installed, the output of this command could be very long and a big pain to scroll up (sometimes it's not even possible to scroll that far back).

In this case, pipe the output to the more program, like this

npm list -g | more
Spitzer answered 11/5, 2016 at 22:48 Comment(0)
A
16

The easiest way would be to do

npm list -g

to list the package and view their installed location.

I had installed npm via chololatey, so the location is

C:\MyProgramData\chocolatey\lib\nodejs.commandline.0.10.31\tools\node_modules

C:\MyProgramData\ is chocolatey repo location.

Applaud answered 11/9, 2014 at 5:28 Comment(0)
A
15
  • Echo the config: npm config ls or npm config list

  • Show all the config settings: npm config ls -l or npm config ls --json

  • Print the effective node_modules folder: npm root or npm root -g

  • Print the local prefix: npm prefix or npm prefix -g

    (This is the closest parent directory to contain a package.json file or node_modules directory)


Attaboy answered 13/10, 2020 at 13:40 Comment(0)
K
12

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

  • /etc/npmrc
  • /home/youruser/.npmrc
  • /root/.npmrc
  • ./.npmrc in the current directory next to package.json file (thanks to @CyrillePontvieux)

on windows:

  • c/Program\ Files/nodejs/node_modules/npm/npmrc

Then in this 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.

Krilov answered 5/11, 2019 at 15:51 Comment(1)
/root/.npmrc should never be search for because sudo with npm is a bad practice even if some tutorial mention it. You forgot to mention .npmrc in the directory next to package.json file.Allegedly
H
10

You can find globally installed modules by the command

npm list -g

It will provide you the location where node.js modules have been installed.

C:\Users\[Username]\AppData\Roaming\npm

If you install node.js modules locally in a folder, you can type the following command to see the location.

npm list
Hanlon answered 30/10, 2017 at 4:42 Comment(0)
A
9

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
Afton answered 27/2, 2019 at 19:30 Comment(0)
H
4

From the docs:

In npm 1.0, there are two ways to install things:

  • 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.

  • locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.

You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).

Hob answered 2/8, 2016 at 13:35 Comment(0)
O
4

From the docs:

Packages are dropped into the node_modules folder under the prefix. When installing locally, this means that you can require("packagename") to load its main module, or require("packagename/lib/path/to/sub/module") to load other modules.

Global installs on Unix systems go to {prefix}/lib/node_modules. Global installs on Windows go to {prefix}/node_modules (that is, no lib folder.)

Scoped packages are installed the same way, except they are grouped together in a sub-folder of the relevant node_modules folder with the name of that scope prefix by the @ symbol, e.g. npm install @myorg/package would place the package in {prefix}/node_modules/@myorg/package. See scope for more details.

If you wish to require() a package, then install it locally.

You can get your {prefix} with npm config get prefix. (Useful when you installed node with nvm).

Read about locally.
Read about globally.

Opinion answered 25/8, 2016 at 9:54 Comment(0)
I
4

Windows 10: When I ran npm prefix -g, I noticed that the install location was inside of the git shell's path that I used to install. Even when that location was added to the path, the command from the globally installed package would not be recognized. Fixed by:

  1. running npm config edit
  2. changing the prefix to 'C:\Users\username\AppData\Roaming\npm'
  3. adding that path to the system path variable
  4. reinstalling the package with -g.
Isoclinal answered 17/10, 2018 at 21:22 Comment(1)
Thanks sir, this was the real problem I was stuck intoKathyrnkati
C
3

In Ubuntu 14.04 they are installed at

/usr/lib/node_modules

Cornetcy answered 3/7, 2016 at 20:16 Comment(0)
P
2

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

Note:

If you are somewhere in folder type cd .. until you are in C: directory. Then, type cd %USERPROFILE%\AppData\Roaming\npm\node_modules. Then, magically, %USERPROFILE% will change into Users\YourUserProfile\.

I just wanted to clarify on ideas referred by Decko in the first response. npm list -g will list all the bits you have globally installed. If you need to find your project related npm package then cd 'your angular project xyz', then run npm list. It will show list of modules in npm package. It will also give you list of dependencies missing, and you may require to effectively run that project.

Prohibitionist answered 13/2, 2019 at 21:21 Comment(1)
Note that if have a fresh install of npm on a Windows system (not sure about linux), npm won't actually make the %USERPROFILE%\AppData\Roaming\npm\node_modules folder until you install a global package for the first timeJustitia
T
1

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)
    }
}
Terylene answered 10/9, 2018 at 14:24 Comment(3)
How should this work? For instance the Node binary is at /usr/bin/node, but obviously (since this is the bin) modules are not there; instead they are at /usr/lib/node_modules.Neap
Updated with reference to npm implementationTerylene
Now this is a different story; you bring in the platform (good) and the respective env variables (despite not mentioning what important functions such as moduleName are doing, but I guess the average reader will be able to infer). Thanks for the modification / correction!Neap
L
1

Btw, npm will look for node_modules in parent folders (up to very root) if can not find in local.

Lands answered 11/1, 2019 at 21:56 Comment(0)
N
1

If you have Visual Studio installed, you will find it comes with its own copy of node separate from the one that is on the path when you installed node yourself - Mine is in C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\NodeJs.

If you run the npm command from inside this directory you will find out which node modules are installed inside visual studio.

Ns answered 22/10, 2019 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.