11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: folders
31cb0ef41Sopenharmony_cisection: 5
41cb0ef41Sopenharmony_cidescription: Folder Structures Used by npm
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Description
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cinpm puts various things on your computer.  That's its job.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciThis document will tell you what it puts where.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#### tl;dr
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci* Local install (default): puts stuff in `./node_modules` of the current
161cb0ef41Sopenharmony_ci  package root.
171cb0ef41Sopenharmony_ci* Global install (with `-g`): puts stuff in /usr/local or wherever node
181cb0ef41Sopenharmony_ci  is installed.
191cb0ef41Sopenharmony_ci* Install it **locally** if you're going to `require()` it.
201cb0ef41Sopenharmony_ci* Install it **globally** if you're going to run it on the command line.
211cb0ef41Sopenharmony_ci* If you need both, then install it in both places, or use `npm link`.
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#### prefix Configuration
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciThe [`prefix` config](/using-npm/config#prefix) defaults to the location where
261cb0ef41Sopenharmony_cinode is installed. On most systems, this is `/usr/local`. On Windows, it's
271cb0ef41Sopenharmony_ci`%AppData%\npm`. On Unix systems, it's one level up, since node is typically
281cb0ef41Sopenharmony_ciinstalled at `{prefix}/bin/node` rather than `{prefix}/node.exe`.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciWhen the `global` flag is set, npm installs things into this prefix.
311cb0ef41Sopenharmony_ciWhen it is not set, it uses the root of the current package, or the
321cb0ef41Sopenharmony_cicurrent working directory if not in a package already.
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#### Node Modules
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciPackages are dropped into the `node_modules` folder under the `prefix`.
371cb0ef41Sopenharmony_ciWhen installing locally, this means that you can
381cb0ef41Sopenharmony_ci`require("packagename")` to load its main module, or
391cb0ef41Sopenharmony_ci`require("packagename/lib/path/to/sub/module")` to load other modules.
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciGlobal installs on Unix systems go to `{prefix}/lib/node_modules`.
421cb0ef41Sopenharmony_ciGlobal installs on Windows go to `{prefix}/node_modules` (that is, no
431cb0ef41Sopenharmony_ci`lib` folder.)
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciScoped packages are installed the same way, except they are grouped together
461cb0ef41Sopenharmony_ciin a sub-folder of the relevant `node_modules` folder with the name of that
471cb0ef41Sopenharmony_ciscope prefix by the @ symbol, e.g. `npm install @myorg/package` would place
481cb0ef41Sopenharmony_cithe package in `{prefix}/node_modules/@myorg/package`. See
491cb0ef41Sopenharmony_ci[`scope`](/using-npm/scope) for more details.
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciIf you wish to `require()` a package, then install it locally.
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci#### Executables
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciWhen in global mode, executables are linked into `{prefix}/bin` on Unix,
561cb0ef41Sopenharmony_cior directly into `{prefix}` on Windows.  Ensure that path is in your
571cb0ef41Sopenharmony_citerminal's `PATH` environment to run them.
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciWhen in local mode, executables are linked into
601cb0ef41Sopenharmony_ci`./node_modules/.bin` so that they can be made available to scripts run
611cb0ef41Sopenharmony_cithrough npm.  (For example, so that a test runner will be in the path
621cb0ef41Sopenharmony_ciwhen you run `npm test`.)
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci#### Man Pages
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciWhen in global mode, man pages are linked into `{prefix}/share/man`.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciWhen in local mode, man pages are not installed.
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciMan pages are not installed on Windows systems.
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci#### Cache
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciSee [`npm cache`](/commands/npm-cache).  Cache files are stored in `~/.npm` on Posix, or
751cb0ef41Sopenharmony_ci`%LocalAppData%/npm-cache` on Windows.
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciThis is controlled by the [`cache` config](/using-npm/config#cache) param.
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci#### Temp Files
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciTemporary files are stored by default in the folder specified by the
821cb0ef41Sopenharmony_ci[`tmp` config](/using-npm/config#tmp), which defaults to the TMPDIR, TMP, or
831cb0ef41Sopenharmony_ciTEMP environment variables, or `/tmp` on Unix and `c:\windows\temp` on Windows.
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciTemp files are given a unique folder under this root for each run of the
861cb0ef41Sopenharmony_ciprogram, and are deleted upon successful exit.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci### More Information
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciWhen installing locally, npm first tries to find an appropriate
911cb0ef41Sopenharmony_ci`prefix` folder.  This is so that `npm install foo@1.2.3` will install
921cb0ef41Sopenharmony_cito the sensible root of your package, even if you happen to have `cd`ed
931cb0ef41Sopenharmony_ciinto some other folder.
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciStarting at the $PWD, npm will walk up the folder tree checking for a
961cb0ef41Sopenharmony_cifolder that contains either a `package.json` file, or a `node_modules`
971cb0ef41Sopenharmony_cifolder.  If such a thing is found, then that is treated as the effective
981cb0ef41Sopenharmony_ci"current directory" for the purpose of running npm commands.  (This
991cb0ef41Sopenharmony_cibehavior is inspired by and similar to git's .git-folder seeking
1001cb0ef41Sopenharmony_cilogic when running git commands in a working dir.)
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciIf no package root is found, then the current folder is used.
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciWhen you run `npm install foo@1.2.3`, then the package is loaded into
1051cb0ef41Sopenharmony_cithe cache, and then unpacked into `./node_modules/foo`.  Then, any of
1061cb0ef41Sopenharmony_cifoo's dependencies are similarly unpacked into
1071cb0ef41Sopenharmony_ci`./node_modules/foo/node_modules/...`.
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciAny bin files are symlinked to `./node_modules/.bin/`, so that they may
1101cb0ef41Sopenharmony_cibe found by npm scripts when necessary.
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci#### Global Installation
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciIf the [`global` config](/using-npm/config#global) is set to true, then npm will
1151cb0ef41Sopenharmony_ciinstall packages "globally".
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciFor global installation, packages are installed roughly the same way,
1181cb0ef41Sopenharmony_cibut using the folders described above.
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci#### Cycles, Conflicts, and Folder Parsimony
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ciCycles are handled using the property of node's module system that it
1231cb0ef41Sopenharmony_ciwalks up the directories looking for `node_modules` folders.  So, at every
1241cb0ef41Sopenharmony_cistage, if a package is already installed in an ancestor `node_modules`
1251cb0ef41Sopenharmony_cifolder, then it is not installed at the current location.
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ciConsider the case above, where `foo -> bar -> baz`.  Imagine if, in
1281cb0ef41Sopenharmony_ciaddition to that, baz depended on bar, so you'd have:
1291cb0ef41Sopenharmony_ci`foo -> bar -> baz -> bar -> baz ...`.  However, since the folder
1301cb0ef41Sopenharmony_cistructure is: `foo/node_modules/bar/node_modules/baz`, there's no need to
1311cb0ef41Sopenharmony_ciput another copy of bar into `.../baz/node_modules`, since when baz calls
1321cb0ef41Sopenharmony_ci`require("bar")`, it will get the copy that is installed in
1331cb0ef41Sopenharmony_ci`foo/node_modules/bar`.
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ciThis shortcut is only used if the exact same
1361cb0ef41Sopenharmony_civersion would be installed in multiple nested `node_modules` folders.  It
1371cb0ef41Sopenharmony_ciis still possible to have `a/node_modules/b/node_modules/a` if the two
1381cb0ef41Sopenharmony_ci"a" packages are different versions.  However, without repeating the
1391cb0ef41Sopenharmony_ciexact same package multiple times, an infinite regress will always be
1401cb0ef41Sopenharmony_ciprevented.
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciAnother optimization can be made by installing dependencies at the
1431cb0ef41Sopenharmony_cihighest level possible, below the localized "target" folder (hoisting).
1441cb0ef41Sopenharmony_ciSince version 3, npm hoists dependencies by default.
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci#### Example
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ciConsider this dependency graph:
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci```bash
1511cb0ef41Sopenharmony_cifoo
1521cb0ef41Sopenharmony_ci+-- blerg@1.2.5
1531cb0ef41Sopenharmony_ci+-- bar@1.2.3
1541cb0ef41Sopenharmony_ci|   +-- blerg@1.x (latest=1.3.7)
1551cb0ef41Sopenharmony_ci|   +-- baz@2.x
1561cb0ef41Sopenharmony_ci|   |   `-- quux@3.x
1571cb0ef41Sopenharmony_ci|   |       `-- bar@1.2.3 (cycle)
1581cb0ef41Sopenharmony_ci|   `-- asdf@*
1591cb0ef41Sopenharmony_ci`-- baz@1.2.3
1601cb0ef41Sopenharmony_ci    `-- quux@3.x
1611cb0ef41Sopenharmony_ci        `-- bar
1621cb0ef41Sopenharmony_ci```
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ciIn this case, we might expect a folder structure like this
1651cb0ef41Sopenharmony_ci(with all dependencies hoisted to the highest level possible):
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci```bash
1681cb0ef41Sopenharmony_cifoo
1691cb0ef41Sopenharmony_ci+-- node_modules
1701cb0ef41Sopenharmony_ci    +-- blerg (1.2.5) <---[A]
1711cb0ef41Sopenharmony_ci    +-- bar (1.2.3) <---[B]
1721cb0ef41Sopenharmony_ci    |   +-- node_modules
1731cb0ef41Sopenharmony_ci    |       +-- baz (2.0.2) <---[C]
1741cb0ef41Sopenharmony_ci    +-- asdf (2.3.4)
1751cb0ef41Sopenharmony_ci    +-- baz (1.2.3) <---[D]
1761cb0ef41Sopenharmony_ci    +-- quux (3.2.0) <---[E]
1771cb0ef41Sopenharmony_ci```
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ciSince foo depends directly on `bar@1.2.3` and `baz@1.2.3`, those are
1801cb0ef41Sopenharmony_ciinstalled in foo's `node_modules` folder.
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ciEven though the latest copy of blerg is 1.3.7, foo has a specific
1831cb0ef41Sopenharmony_cidependency on version 1.2.5.  So, that gets installed at [A].  Since the
1841cb0ef41Sopenharmony_ciparent installation of blerg satisfies bar's dependency on `blerg@1.x`,
1851cb0ef41Sopenharmony_ciit does not install another copy under [B].
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ciBar [B] also has dependencies on baz and asdf.  Because it depends on `baz@2.x`, it cannot
1881cb0ef41Sopenharmony_cire-use the `baz@1.2.3` installed in the parent `node_modules` folder [D],
1891cb0ef41Sopenharmony_ciand must install its own copy [C]. In order to minimize duplication, npm hoists
1901cb0ef41Sopenharmony_cidependencies to the top level by default, so asdf is installed under [A].
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ciUnderneath bar, the `baz -> quux -> bar` dependency creates a cycle.
1931cb0ef41Sopenharmony_ciHowever, because bar is already in quux's ancestry [B], it does not
1941cb0ef41Sopenharmony_ciunpack another copy of bar into that folder. Likewise, quux's [E]
1951cb0ef41Sopenharmony_cifolder tree is empty, because its dependency on bar is satisfied
1961cb0ef41Sopenharmony_ciby the parent folder copy installed at [B].
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ciFor a graphical breakdown of what is installed where, use `npm ls`.
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci#### Publishing
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ciUpon publishing, npm will look in the `node_modules` folder.  If any of
2031cb0ef41Sopenharmony_cithe items there are not in the `bundleDependencies` array, then they will
2041cb0ef41Sopenharmony_cinot be included in the package tarball.
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ciThis allows a package maintainer to install all of their dependencies
2071cb0ef41Sopenharmony_ci(and dev dependencies) locally, but only re-publish those items that
2081cb0ef41Sopenharmony_cicannot be found elsewhere.  See [`package.json`](/configuring-npm/package-json) for more information.
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ci### See also
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci* [package.json](/configuring-npm/package-json)
2131cb0ef41Sopenharmony_ci* [npm install](/commands/npm-install)
2141cb0ef41Sopenharmony_ci* [npm pack](/commands/npm-pack)
2151cb0ef41Sopenharmony_ci* [npm cache](/commands/npm-cache)
2161cb0ef41Sopenharmony_ci* [npm config](/commands/npm-config)
2171cb0ef41Sopenharmony_ci* [npmrc](/configuring-npm/npmrc)
2181cb0ef41Sopenharmony_ci* [config](/using-npm/config)
2191cb0ef41Sopenharmony_ci* [npm publish](/commands/npm-publish)
220