1 .TH "NPM\-FOLDERS" "5" "March 2015" "" ""
3 \fBnpm-folders\fR \- Folder Structures Used by npm
6 npm puts various things on your computer\. That's its job\.
8 This document will tell you what it puts where\.
12 Local install (default): puts stuff in \fB\|\./node_modules\fR of the current
15 Global install (with \fB\-g\fR): puts stuff in /usr/local or wherever node
18 Install it \fBlocally\fR if you're going to \fBrequire()\fR it\.
20 Install it \fBglobally\fR if you're going to run it on the command line\.
22 If you need both, then install it in both places, or use \fBnpm link\fR\|\.
25 .SS prefix Configuration
27 The \fBprefix\fR config defaults to the location where node is installed\.
28 On most systems, this is \fB/usr/local\fR, and most of the time is the same
29 as node's \fBprocess\.installPrefix\fR\|\.
31 On windows, this is the exact location of the node\.exe binary\. On Unix
32 systems, it's one level up, since node is typically installed at
33 \fB{prefix}/bin/node\fR rather than \fB{prefix}/node\.exe\fR\|\.
35 When the \fBglobal\fR flag is set, npm installs things into this prefix\.
36 When it is not set, it uses the root of the current package, or the
37 current working directory if not in a package already\.
40 Packages are dropped into the \fBnode_modules\fR folder under the \fBprefix\fR\|\.
41 When installing locally, this means that you can
42 \fBrequire("packagename")\fR to load its main module, or
43 \fBrequire("packagename/lib/path/to/sub/module")\fR to load other modules\.
45 Global installs on Unix systems go to \fB{prefix}/lib/node_modules\fR\|\.
46 Global installs on Windows go to \fB{prefix}/node_modules\fR (that is, no
49 Scoped packages are installed the same way, except they are grouped together
50 in a sub\-folder of the relevant \fBnode_modules\fR folder with the name of that
51 scope prefix by the @ symbol, e\.g\. \fBnpm install @myorg/package\fR would place
52 the package in \fB{prefix}/node_modules/@myorg/package\fR\|\. See npm help 7 \fBscopes\fR for
55 If you wish to \fBrequire()\fR a package, then install it locally\.
58 When in global mode, executables are linked into \fB{prefix}/bin\fR on Unix,
59 or directly into \fB{prefix}\fR on Windows\.
61 When in local mode, executables are linked into
62 \fB\|\./node_modules/\.bin\fR so that they can be made available to scripts run
63 through npm\. (For example, so that a test runner will be in the path
64 when you run \fBnpm test\fR\|\.)
67 When in global mode, man pages are linked into \fB{prefix}/share/man\fR\|\.
69 When in local mode, man pages are not installed\.
71 Man pages are not installed on Windows systems\.
74 See npm help \fBnpm\-cache\fR\|\. Cache files are stored in \fB~/\.npm\fR on Posix, or
75 \fB~/npm\-cache\fR on Windows\.
77 This is controlled by the \fBcache\fR configuration param\.
80 Temporary files are stored by default in the folder specified by the
81 \fBtmp\fR config, which defaults to the TMPDIR, TMP, or TEMP environment
82 variables, or \fB/tmp\fR on Unix and \fBc:\\windows\\temp\fR on Windows\.
84 Temp files are given a unique folder under this root for each run of the
85 program, and are deleted upon successful exit\.
88 When installing locally, npm first tries to find an appropriate
89 \fBprefix\fR folder\. This is so that \fBnpm install foo@1\.2\.3\fR will install
90 to the sensible root of your package, even if you happen to have \fBcd\fRed
91 into some other folder\.
93 Starting at the $PWD, npm will walk up the folder tree checking for a
94 folder that contains either a \fBpackage\.json\fR file, or a \fBnode_modules\fR
95 folder\. If such a thing is found, then that is treated as the effective
96 "current directory" for the purpose of running npm commands\. (This
97 behavior is inspired by and similar to git's \.git\-folder seeking
98 logic when running git commands in a working dir\.)
100 If no package root is found, then the current folder is used\.
102 When you run \fBnpm install foo@1\.2\.3\fR, then the package is loaded into
103 the cache, and then unpacked into \fB\|\./node_modules/foo\fR\|\. Then, any of
104 foo's dependencies are similarly unpacked into
105 \fB\|\./node_modules/foo/node_modules/\.\.\.\fR\|\.
107 Any bin files are symlinked to \fB\|\./node_modules/\.bin/\fR, so that they may
108 be found by npm scripts when necessary\.
109 .SS Global Installation
111 If the \fBglobal\fR configuration is set to true, then npm will
112 install packages "globally"\.
114 For global installation, packages are installed roughly the same way,
115 but using the folders described above\.
116 .SS Cycles, Conflicts, and Folder Parsimony
118 Cycles are handled using the property of node's module system that it
119 walks up the directories looking for \fBnode_modules\fR folders\. So, at every
120 stage, if a package is already installed in an ancestor \fBnode_modules\fR
121 folder, then it is not installed at the current location\.
123 Consider the case above, where \fBfoo \-> bar \-> baz\fR\|\. Imagine if, in
124 addition to that, baz depended on bar, so you'd have:
125 \fBfoo \-> bar \-> baz \-> bar \-> baz \.\.\.\fR\|\. However, since the folder
126 structure is: \fBfoo/node_modules/bar/node_modules/baz\fR, there's no need to
127 put another copy of bar into \fB\|\.\.\./baz/node_modules\fR, since when it calls
128 require("bar"), it will get the copy that is installed in
129 \fBfoo/node_modules/bar\fR\|\.
131 This shortcut is only used if the exact same
132 version would be installed in multiple nested \fBnode_modules\fR folders\. It
133 is still possible to have \fBa/node_modules/b/node_modules/a\fR if the two
134 "a" packages are different versions\. However, without repeating the
135 exact same package multiple times, an infinite regress will always be
138 Another optimization can be made by installing dependencies at the
139 highest level possible, below the localized "target" folder\.
142 Consider this dependency graph:
149 | +\-\- blerg@1\.x (latest=1\.3\.7)
152 | | `\-\- bar@1\.2\.3 (cycle)
160 In this case, we might expect a folder structure like this:
166 +\-\- blerg (1\.2\.5) <\-\-\-[A]
167 +\-\- bar (1\.2\.3) <\-\-\-[B]
169 | +\-\- baz (2\.0\.2) <\-\-\-[C]
170 | | `\-\- node_modules
171 | | `\-\- quux (3\.2\.0)
172 | `\-\- asdf (2\.3\.4)
173 `\-\- baz (1\.2\.3) <\-\-\-[D]
175 `\-\- quux (3\.2\.0) <\-\-\-[E]
179 Since foo depends directly on \fBbar@1\.2\.3\fR and \fBbaz@1\.2\.3\fR, those are
180 installed in foo's \fBnode_modules\fR folder\.
182 Even though the latest copy of blerg is 1\.3\.7, foo has a specific
183 dependency on version 1\.2\.5\. So, that gets installed at [A]\. Since the
184 parent installation of blerg satisfies bar's dependency on \fBblerg@1\.x\fR,
185 it does not install another copy under [B]\.
187 Bar [B] also has dependencies on baz and asdf, so those are installed in
188 bar's \fBnode_modules\fR folder\. Because it depends on \fBbaz@2\.x\fR, it cannot
189 re\-use the \fBbaz@1\.2\.3\fR installed in the parent \fBnode_modules\fR folder [D],
190 and must install its own copy [C]\.
192 Underneath bar, the \fBbaz \-> quux \-> bar\fR dependency creates a cycle\.
193 However, because bar is already in quux's ancestry [B], it does not
194 unpack another copy of bar into that folder\.
196 Underneath \fBfoo \-> baz\fR [D], quux's [E] folder tree is empty, because its
197 dependency on bar is satisfied by the parent folder copy installed at [B]\.
199 For a graphical breakdown of what is installed where, use \fBnpm ls\fR\|\.
202 Upon publishing, npm will look in the \fBnode_modules\fR folder\. If any of
203 the items there are not in the \fBbundledDependencies\fR array, then they will
204 not be included in the package tarball\.
206 This allows a package maintainer to install all of their dependencies
207 (and dev dependencies) locally, but only re\-publish those items that
208 cannot be found elsewhere\. See npm help 5 \fBpackage\.json\fR for more information\.
214 npm help 5 package\.json