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