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 cur‐
15           rent 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 ⟨/using-npm/config#prefix⟩ defaults to the location
29       where node is installed. On most systems, this is /usr/local.  On  Win‐
30       dows,  it's  %AppData%\npm.  On  Unix systems, it's one level up, since
31       node is typically installed  at  {prefix}/bin/node  rather  than  {pre‐
32       fix}/node.exe.
33
34       When the global flag is set, npm installs things into this prefix. When
35       it is not set, it uses the root of the current package, or the  current
36       working directory if not in a package already.
37
38   Node Modules
39       Packages  are  dropped  into  the node_modules folder under the prefix.
40       When installing locally, this means that you can require("packagename")
41       to  load  its main module, or require("packagename/lib/path/to/sub/mod‐
42       ule") to load other modules.
43
44       Global installs on Unix systems go to {prefix}/lib/node_modules. Global
45       installs  on  Windows  go  to  {prefix}/node_modules  (that  is, no lib
46       folder.)
47
48       Scoped packages are installed the same way, except they are grouped to‐
49       gether  in  a  sub-folder  of the relevant node_modules folder with the
50       name of that scope prefix by  the  @  symbol,  e.g.  npm  install  @my‐
51       org/package  would  place  the  package  in  {prefix}/node_modules/@my‐
52       org/package. See npm help scope for more details.
53
54       If you wish to require() a package, then install it locally.
55
56   Executables
57       When in global mode, executables are linked into {prefix}/bin on  Unix,
58       or  directly into {prefix} on Windows. Ensure that path is in your ter‐
59       minal's PATH environment to run them.
60
61       When in local mode, executables are linked into ./node_modules/.bin  so
62       that  they can be made available to scripts run through npm. (For exam‐
63       ple, so that a test runner will be in the path when you run npm test.)
64
65   Man Pages
66       When in global mode, man pages are linked into {prefix}/share/man.
67
68       When in local mode, man pages are not installed.
69
70       Man pages are not installed on Windows systems.
71
72   Cache
73       See npm help cache. Cache files are stored in ~/.npm on Posix, or  %Lo‐
74       calAppData%/npm-cache on Windows.
75
76       This is controlled by the cache config ⟨/using-npm/config#cache⟩ param.
77
78   Temp Files
79       Temporary  files  are  stored by default in the folder specified by the
80       tmp config ⟨/using-npm/config#tmp⟩, which defaults to the TMPDIR,  TMP,
81       or  TEMP  environment variables, or /tmp on Unix and c:\windows\temp on
82       Windows.
83
84       Temp files are given a unique folder under this root for  each  run  of
85       the program, and are deleted upon successful exit.
86
87   More Information
88       When  installing locally, npm first tries to find an appropriate prefix
89       folder. This is so that npm install foo@1.2.3 will install to the  sen‐
90       sible  root  of your package, even if you happen to have cded into some
91       other folder.
92
93       Starting at the $PWD, npm will walk up the folder tree checking  for  a
94       folder  that  contains  either  a  package.json file, or a node_modules
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 be‐
97       havior is inspired by and similar to git's  .git-folder  seeking  logic
98       when running git commands in a working dir.)
99
100       If no package root is found, then the current folder is used.
101
102       When you run npm install foo@1.2.3, then the package is loaded into the
103       cache, and then unpacked into ./node_modules/foo. Then,  any  of  foo's
104       dependencies  are  similarly unpacked into ./node_modules/foo/node_mod‐
105       ules/....
106
107       Any bin files are symlinked to ./node_modules/.bin/, so that  they  may
108       be found by npm scripts when necessary.
109
110   Global Installation
111       If  the  global  config ⟨/using-npm/config#global⟩ is set to true, then
112       npm will install packages "globally".
113
114       For global installation, packages are installed roughly the  same  way,
115       but using the folders described above.
116
117   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 node_modules folders. So, at every
120       stage,  if  a  package is already installed in an ancestor node_modules
121       folder, then it is not installed at the current location.
122
123       Consider the case above, where foo -> bar -> baz. Imagine if, in  addi‐
124       tion  to that, baz depended on bar, so you'd have: foo -> bar -> baz ->
125       bar -> baz .... However, since the folder structure  is:  foo/node_mod‐
126       ules/bar/node_modules/baz,  there's  no need to put another copy of bar
127       into .../baz/node_modules, since when baz calls require("bar"), it will
128       get the copy that is installed in foo/node_modules/bar.
129
130       This shortcut is only used if the exact same version would be installed
131       in multiple nested node_modules folders. It is still possible  to  have
132       a/node_modules/b/node_modules/a  if  the two "a" packages are different
133       versions. However, without repeating the exact  same  package  multiple
134       times, an infinite regress will always be prevented.
135
136       Another  optimization  can  be  made  by installing dependencies at the
137       highest level possible, below the localized "target" folder (hoisting).
138       Since version 3, npm hoists dependencies by default.
139
140   Example
141       Consider this dependency graph:
142
143         foo
144         +-- blerg@1.2.5
145         +-- bar@1.2.3
146         |   +-- blerg@1.x (latest=1.3.7)
147         |   +-- baz@2.x
148         |   |   `-- quux@3.x
149         |   |       `-- bar@1.2.3 (cycle)
150         |   `-- asdf@*
151         `-- baz@1.2.3
152             `-- quux@3.x
153                 `-- bar
154
155       In  this  case,  we might expect a folder structure like this (with all
156       dependencies hoisted to the highest level possible):
157
158         foo
159         +-- node_modules
160             +-- blerg (1.2.5) <---[A]
161             +-- bar (1.2.3) <---[B]
162             |   +-- node_modules
163             |       +-- baz (2.0.2) <---[C]
164             +-- asdf (2.3.4)
165             +-- baz (1.2.3) <---[D]
166             +-- quux (3.2.0) <---[E]
167
168       Since foo depends directly on bar@1.2.3 and baz@1.2.3,  those  are  in‐
169       stalled in foo's node_modules folder.
170
171       Even  though  the latest copy of blerg is 1.3.7, foo has a specific de‐
172       pendency on version 1.2.5. So, that gets installed at  [A].  Since  the
173       parent  installation  of blerg satisfies bar's dependency on blerg@1.x,
174       it does not install another copy under [B].
175
176       Bar [B] also has dependencies on baz and asdf. Because  it  depends  on
177       baz@2.x,  it  cannot  re-use  the  baz@1.2.3  installed  in  the parent
178       node_modules folder [D], and must install its own copy [C]. In order to
179       minimize  duplication,  npm hoists dependencies to the top level by de‐
180       fault, so asdf is installed under [A].
181
182       Underneath bar, the baz -> quux -> bar dependency creates a cycle. How‐
183       ever, because bar is already in quux's ancestry [B], it does not unpack
184       another copy of bar into that folder. Likewise, quux's [E] folder  tree
185       is  empty,  because  its  dependency  on bar is satisfied by the parent
186       folder copy installed at [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 bundleDependencies array, then they will
193       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 package.json  ⟨/configuring-npm/package-
198       json⟩ for more information.
199
200   See also
201package.json ⟨/configuring-npm/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                                 November 2023                      FOLDERS(5)
Impressum