3 <title>npm-folders</title>
4 <meta http-equiv="content-type" value="text/html;utf-8">
5 <link rel="stylesheet" type="text/css" href="../../static/style.css">
9 <h1><a href="../files/npm-folders.html">npm-folders</a></h1> <p>Folder Structures Used by npm</p>
11 <h2 id="DESCRIPTION">DESCRIPTION</h2>
13 <p>npm puts various things on your computer. That's its job.</p>
15 <p>This document will tell you what it puts where.</p>
17 <h3 id="tl-dr">tl;dr</h3>
19 <ul><li>Local install (default): puts stuff in <code>./node_modules</code> of the current
20 package root.</li><li>Global install (with <code>-g</code>): puts stuff in /usr/local or wherever node
21 is installed.</li><li>Install it <strong>locally</strong> if you're going to <code>require()</code> it.</li><li>Install it <strong>globally</strong> if you're going to run it on the command line.</li><li>If you need both, then install it in both places, or use <code>npm link</code>.</li></ul>
23 <h3 id="prefix-Configuration">prefix Configuration</h3>
25 <p>The <code>prefix</code> config defaults to the location where node is installed.
26 On most systems, this is <code>/usr/local</code>, and most of the time is the same
27 as node's <code>process.installPrefix</code>.</p>
29 <p>On windows, this is the exact location of the node.exe binary. On Unix
30 systems, it's one level up, since node is typically installed at
31 <code>{prefix}/bin/node</code> rather than <code>{prefix}/node.exe</code>.</p>
33 <p>When the <code>global</code> flag is set, npm installs things into this prefix.
34 When it is not set, it uses the root of the current package, or the
35 current working directory if not in a package already.</p>
37 <h3 id="Node-Modules">Node Modules</h3>
39 <p>Packages are dropped into the <code>node_modules</code> folder under the <code>prefix</code>.
40 When installing locally, this means that you can
41 <code>require("packagename")</code> to load its main module, or
42 <code>require("packagename/lib/path/to/sub/module")</code> to load other modules.</p>
44 <p>Global installs on Unix systems go to <code>{prefix}/lib/node_modules</code>.
45 Global installs on Windows go to <code>{prefix}/node_modules</code> (that is, no
46 <code>lib</code> folder.)</p>
48 <p>If you wish to <code>require()</code> a package, then install it locally.</p>
50 <h3 id="Executables">Executables</h3>
52 <p>When in global mode, executables are linked into <code>{prefix}/bin</code> on Unix,
53 or directly into <code>{prefix}</code> on Windows.</p>
55 <p>When in local mode, executables are linked into
56 <code>./node_modules/.bin</code> so that they can be made available to scripts run
57 through npm. (For example, so that a test runner will be in the path
58 when you run <code>npm test</code>.)</p>
60 <h3 id="Man-Pages">Man Pages</h3>
62 <p>When in global mode, man pages are linked into <code>{prefix}/share/man</code>.</p>
64 <p>When in local mode, man pages are not installed.</p>
66 <p>Man pages are not installed on Windows systems.</p>
68 <h3 id="Cache">Cache</h3>
70 <p>See <code><a href="../cli/npm-cache.html">npm-cache(1)</a></code>. Cache files are stored in <code>~/.npm</code> on Posix, or
71 <code>~/npm-cache</code> on Windows.</p>
73 <p>This is controlled by the <code>cache</code> configuration param.</p>
75 <h3 id="Temp-Files">Temp Files</h3>
77 <p>Temporary files are stored by default in the folder specified by the
78 <code>tmp</code> config, which defaults to the TMPDIR, TMP, or TEMP environment
79 variables, or <code>/tmp</code> on Unix and <code>c:\windows\temp</code> on Windows.</p>
81 <p>Temp files are given a unique folder under this root for each run of the
82 program, and are deleted upon successful exit.</p>
84 <h2 id="More-Information">More Information</h2>
86 <p>When installing locally, npm first tries to find an appropriate
87 <code>prefix</code> folder. This is so that <code>npm install foo@1.2.3</code> will install
88 to the sensible root of your package, even if you happen to have <code>cd</code>ed
89 into some other folder.</p>
91 <p>Starting at the $PWD, npm will walk up the folder tree checking for a
92 folder that contains either a <code>package.json</code> file, or a <code>node_modules</code>
93 folder. If such a thing is found, then that is treated as the effective
94 "current directory" for the purpose of running npm commands. (This
95 behavior is inspired by and similar to git's .git-folder seeking
96 logic when running git commands in a working dir.)</p>
98 <p>If no package root is found, then the current folder is used.</p>
100 <p>When you run <code>npm install foo@1.2.3</code>, then the package is loaded into
101 the cache, and then unpacked into <code>./node_modules/foo</code>. Then, any of
102 foo's dependencies are similarly unpacked into
103 <code>./node_modules/foo/node_modules/...</code>.</p>
105 <p>Any bin files are symlinked to <code>./node_modules/.bin/</code>, so that they may
106 be found by npm scripts when necessary.</p>
108 <h3 id="Global-Installation">Global Installation</h3>
110 <p>If the <code>global</code> configuration is set to true, then npm will
111 install packages "globally".</p>
113 <p>For global installation, packages are installed roughly the same way,
114 but using the folders described above.</p>
116 <h3 id="Cycles-Conflicts-and-Folder-Parsimony">Cycles, Conflicts, and Folder Parsimony</h3>
118 <p>Cycles are handled using the property of node's module system that it
119 walks up the directories looking for <code>node_modules</code> folders. So, at every
120 stage, if a package is already installed in an ancestor <code>node_modules</code>
121 folder, then it is not installed at the current location.</p>
123 <p>Consider the case above, where <code>foo -> bar -> baz</code>. Imagine if, in
124 addition to that, baz depended on bar, so you'd have:
125 <code>foo -> bar -> baz -> bar -> baz ...</code>. However, since the folder
126 structure is: <code>foo/node_modules/bar/node_modules/baz</code>, there's no need to
127 put another copy of bar into <code>.../baz/node_modules</code>, since when it calls
128 require("bar"), it will get the copy that is installed in
129 <code>foo/node_modules/bar</code>.</p>
131 <p>This shortcut is only used if the exact same
132 version would be installed in multiple nested <code>node_modules</code> folders. It
133 is still possible to have <code>a/node_modules/b/node_modules/a</code> 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 <p>Another optimization can be made by installing dependencies at the
139 highest level possible, below the localized "target" folder.</p>
141 <h4 id="Example">Example</h4>
143 <p>Consider this dependency graph:</p>
148 | +-- blerg@1.x (latest=1.3.7)
151 | | `-- bar@1.2.3 (cycle)
157 <p>In this case, we might expect a folder structure like this:</p>
161 +-- blerg (1.2.5) <---[A]
162 +-- bar (1.2.3) <---[B]
164 | +-- baz (2.0.2) <---[C]
168 `-- baz (1.2.3) <---[D]
170 `-- quux (3.2.0) <---[E]</code></pre>
172 <p>Since foo depends directly on <code>bar@1.2.3</code> and <code>baz@1.2.3</code>, those are
173 installed in foo's <code>node_modules</code> folder.</p>
175 <p>Even though the latest copy of blerg is 1.3.7, foo has a specific
176 dependency on version 1.2.5. So, that gets installed at [A]. Since the
177 parent installation of blerg satisfies bar's dependency on <code>blerg@1.x</code>,
178 it does not install another copy under [B].</p>
180 <p>Bar [B] also has dependencies on baz and asdf, so those are installed in
181 bar's <code>node_modules</code> folder. Because it depends on <code>baz@2.x</code>, it cannot
182 re-use the <code>baz@1.2.3</code> installed in the parent <code>node_modules</code> folder [D],
183 and must install its own copy [C].</p>
185 <p>Underneath bar, the <code>baz -> quux -> bar</code> dependency creates a cycle.
186 However, because bar is already in quux's ancestry [B], it does not
187 unpack another copy of bar into that folder.</p>
189 <p>Underneath <code>foo -> baz</code> [D], quux's [E] folder tree is empty, because its
190 dependency on bar is satisfied by the parent folder copy installed at [B].</p>
192 <p>For a graphical breakdown of what is installed where, use <code>npm ls</code>.</p>
194 <h3 id="Publishing">Publishing</h3>
196 <p>Upon publishing, npm will look in the <code>node_modules</code> folder. If any of
197 the items there are not in the <code>bundledDependencies</code> array, then they will
198 not be included in the package tarball.</p>
200 <p>This allows a package maintainer to install all of their dependencies
201 (and dev dependencies) locally, but only re-publish those items that
202 cannot be found elsewhere. See <code><a href="../files/package.json.html">package.json(5)</a></code> for more information.</p>
204 <h2 id="SEE-ALSO">SEE ALSO</h2>
206 <ul><li><a href="../misc/npm-faq.html">npm-faq(7)</a></li><li><a href="../files/package.json.html">package.json(5)</a></li><li><a href="../cli/npm-install.html">npm-install(1)</a></li><li><a href="../cli/npm-pack.html">npm-pack(1)</a></li><li><a href="../cli/npm-cache.html">npm-cache(1)</a></li><li><a href="../cli/npm-config.html">npm-config(1)</a></li><li><a href="../files/npmrc.html">npmrc(5)</a></li><li><a href="../misc/npm-config.html">npm-config(7)</a></li><li><a href="../cli/npm-publish.html">npm-publish(1)</a></li></ul>
208 <p id="footer">npm-folders — npm@1.3.17</p>
211 var wrapper = document.getElementById("wrapper")
212 var els = Array.prototype.slice.call(wrapper.getElementsByTagName("*"), 0)
213 .filter(function (el) {
214 return el.parentNode === wrapper
215 && el.tagName.match(/H[1-6]/)
219 , toc = document.createElement("ul")
220 toc.innerHTML = els.map(function (el) {
221 var i = el.tagName.charAt(1)
231 out += "<li><a href='#" + el.id + "'>" +
232 ( el.innerText || el.text || el.innerHTML)
237 document.body.appendChild(toc)