1FOLDERS(5)                                                          FOLDERS(5)
2
3
4

NAME

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