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