1--- 2section: configuring-npm 3title: folders 4description: Folder Structures Used by npm 5--- 6 7# folders(5) 8 9## Folder Structures Used by npm 10 11### Description 12 13npm puts various things on your computer. That's its job. 14 15This document will tell you what it puts where. 16 17#### tl;dr 18 19* Local install (default): puts stuff in `./node_modules` of the current 20 package root. 21* Global install (with `-g`): puts stuff in /usr/local or wherever node 22 is installed. 23* Install it **locally** if you're going to `require()` it. 24* Install it **globally** if you're going to run it on the command line. 25* If you need both, then install it in both places, or use `npm link`. 26 27#### prefix Configuration 28 29The `prefix` config defaults to the location where node is installed. 30On most systems, this is `/usr/local`. On Windows, it's `%AppData%\npm`. 31On Unix systems, it's one level up, since node is typically installed at 32`{prefix}/bin/node` rather than `{prefix}/node.exe`. 33 34When the `global` flag is set, npm installs things into this prefix. 35When it is not set, it uses the root of the current package, or the 36current working directory if not in a package already. 37 38#### Node Modules 39 40Packages are dropped into the `node_modules` folder under the `prefix`. 41When installing locally, this means that you can 42`require("packagename")` to load its main module, or 43`require("packagename/lib/path/to/sub/module")` to load other modules. 44 45Global installs on Unix systems go to `{prefix}/lib/node_modules`. 46Global installs on Windows go to `{prefix}/node_modules` (that is, no 47`lib` folder.) 48 49Scoped packages are installed the same way, except they are grouped together 50in a sub-folder of the relevant `node_modules` folder with the name of that 51scope prefix by the @ symbol, e.g. `npm install @myorg/package` would place 52the package in `{prefix}/node_modules/@myorg/package`. See [`scope`](/using-npm/scope) for more details. 53 54If you wish to `require()` a package, then install it locally. 55 56#### Executables 57 58When in global mode, executables are linked into `{prefix}/bin` on Unix, 59or directly into `{prefix}` on Windows. 60 61When in local mode, executables are linked into 62`./node_modules/.bin` so that they can be made available to scripts run 63through npm. (For example, so that a test runner will be in the path 64when you run `npm test`.) 65 66#### Man Pages 67 68When in global mode, man pages are linked into `{prefix}/share/man`. 69 70When in local mode, man pages are not installed. 71 72Man pages are not installed on Windows systems. 73 74#### Cache 75 76See [`npm cache`](/cli-commands/npm-cache). Cache files are stored in `~/.npm` on Posix, or 77`%AppData%/npm-cache` on Windows. 78 79This is controlled by the `cache` configuration param. 80 81#### Temp Files 82 83Temporary files are stored by default in the folder specified by the 84`tmp` config, which defaults to the TMPDIR, TMP, or TEMP environment 85variables, or `/tmp` on Unix and `c:\windows\temp` on Windows. 86 87Temp files are given a unique folder under this root for each run of the 88program, and are deleted upon successful exit. 89 90### More Information 91 92When installing locally, npm first tries to find an appropriate 93`prefix` folder. This is so that `npm install foo@1.2.3` will install 94to the sensible root of your package, even if you happen to have `cd`ed 95into some other folder. 96 97Starting at the $PWD, npm will walk up the folder tree checking for a 98folder that contains either a `package.json` file, or a `node_modules` 99folder. If such a thing is found, then that is treated as the effective 100"current directory" for the purpose of running npm commands. (This 101behavior is inspired by and similar to git's .git-folder seeking 102logic when running git commands in a working dir.) 103 104If no package root is found, then the current folder is used. 105 106When you run `npm install foo@1.2.3`, then the package is loaded into 107the cache, and then unpacked into `./node_modules/foo`. Then, any of 108foo's dependencies are similarly unpacked into 109`./node_modules/foo/node_modules/...`. 110 111Any bin files are symlinked to `./node_modules/.bin/`, so that they may 112be found by npm scripts when necessary. 113 114#### Global Installation 115 116If the `global` configuration is set to true, then npm will 117install packages "globally". 118 119For global installation, packages are installed roughly the same way, 120but using the folders described above. 121 122#### Cycles, Conflicts, and Folder Parsimony 123 124Cycles are handled using the property of node's module system that it 125walks up the directories looking for `node_modules` folders. So, at every 126stage, if a package is already installed in an ancestor `node_modules` 127folder, then it is not installed at the current location. 128 129Consider the case above, where `foo -> bar -> baz`. Imagine if, in 130addition to that, baz depended on bar, so you'd have: 131`foo -> bar -> baz -> bar -> baz ...`. However, since the folder 132structure is: `foo/node_modules/bar/node_modules/baz`, there's no need to 133put another copy of bar into `.../baz/node_modules`, since when it calls 134require("bar"), it will get the copy that is installed in 135`foo/node_modules/bar`. 136 137This shortcut is only used if the exact same 138version would be installed in multiple nested `node_modules` folders. It 139is still possible to have `a/node_modules/b/node_modules/a` if the two 140"a" packages are different versions. However, without repeating the 141exact same package multiple times, an infinite regress will always be 142prevented. 143 144Another optimization can be made by installing dependencies at the 145highest level possible, below the localized "target" folder. 146 147#### Example 148 149Consider this dependency graph: 150 151```bash 152foo 153+-- blerg@1.2.5 154+-- bar@1.2.3 155| +-- blerg@1.x (latest=1.3.7) 156| +-- baz@2.x 157| | `-- quux@3.x 158| | `-- bar@1.2.3 (cycle) 159| `-- asdf@* 160`-- baz@1.2.3 161 `-- quux@3.x 162 `-- bar 163``` 164 165In this case, we might expect a folder structure like this: 166 167```bash 168foo 169+-- node_modules 170 +-- blerg (1.2.5) <---[A] 171 +-- bar (1.2.3) <---[B] 172 | `-- node_modules 173 | +-- baz (2.0.2) <---[C] 174 | | `-- node_modules 175 | | `-- quux (3.2.0) 176 | `-- asdf (2.3.4) 177 `-- baz (1.2.3) <---[D] 178 `-- node_modules 179 `-- quux (3.2.0) <---[E] 180``` 181 182Since foo depends directly on `bar@1.2.3` and `baz@1.2.3`, those are 183installed in foo's `node_modules` folder. 184 185Even though the latest copy of blerg is 1.3.7, foo has a specific 186dependency on version 1.2.5. So, that gets installed at [A]. Since the 187parent installation of blerg satisfies bar's dependency on `blerg@1.x`, 188it does not install another copy under [B]. 189 190Bar [B] also has dependencies on baz and asdf, so those are installed in 191bar's `node_modules` folder. Because it depends on `baz@2.x`, it cannot 192re-use the `baz@1.2.3` installed in the parent `node_modules` folder [D], 193and must install its own copy [C]. 194 195Underneath bar, the `baz -> quux -> bar` dependency creates a cycle. 196However, because bar is already in quux's ancestry [B], it does not 197unpack another copy of bar into that folder. 198 199Underneath `foo -> baz` [D], quux's [E] folder tree is empty, because its 200dependency on bar is satisfied by the parent folder copy installed at [B]. 201 202For a graphical breakdown of what is installed where, use `npm ls`. 203 204#### Publishing 205 206Upon publishing, npm will look in the `node_modules` folder. If any of 207the items there are not in the `bundledDependencies` array, then they will 208not be included in the package tarball. 209 210This allows a package maintainer to install all of their dependencies 211(and dev dependencies) locally, but only re-publish those items that 212cannot be found elsewhere. See [`package.json`](/configuring-npm/package.json) for more information. 213 214### See also 215 216* [package.json](/configuring-npm/package-json) 217* [npm install](/cli-commands/npm-install) 218* [npm pack](/cli-commands/npm-pack) 219* [npm cache](/cli-commands/npm-cache) 220* [npm config](/cli-commands/npm-config) 221* [npmrc](/configuring-npm/npmrc) 222* [config](/using-npm/config) 223* [npm publish](/cli-commands/npm-publish) 224