@@ -128,8 +128,8 @@ Consider the case above, where `foo -> bar -> baz`. Imagine if, in
128
128
addition to that, baz depended on bar, so you'd have:
129
129
` foo -> bar -> baz -> bar -> baz ... ` . However, since the folder
130
130
structure is: ` foo/node_modules/bar/node_modules/baz ` , there's no need to
131
- put another copy of bar into ` .../baz/node_modules ` , since when it calls
132
- require("bar"), it will get the copy that is installed in
131
+ put another copy of bar into ` .../baz/node_modules ` , since when baz calls
132
+ ` require("bar") ` , it will get the copy that is installed in
133
133
` foo/node_modules/bar ` .
134
134
135
135
This shortcut is only used if the exact same
@@ -140,7 +140,8 @@ exact same package multiple times, an infinite regress will always be
140
140
prevented.
141
141
142
142
Another optimization can be made by installing dependencies at the
143
- highest level possible, below the localized "target" folder.
143
+ highest level possible, below the localized "target" folder (hoisting).
144
+ Since version 5, npm hoists dependencies by default.
144
145
145
146
#### Example
146
147
@@ -160,21 +161,19 @@ foo
160
161
` -- bar
161
162
```
162
163
163
- In this case, we might expect a folder structure like this:
164
+ In this case, we might expect a folder structure like this
165
+ (with all dependencies hoisted to the highest level possible):
164
166
165
167
``` bash
166
168
foo
167
169
+-- node_modules
168
170
+-- blerg (1.2.5) < ---[A]
169
171
+-- bar (1.2.3) < ---[B]
170
- | ` -- node_modules
172
+ | + -- node_modules
171
173
| +-- baz (2.0.2) < ---[C]
172
- | | ` -- node_modules
173
- | | ` -- quux (3.2.0)
174
- | ` -- asdf (2.3.4)
175
- ` -- baz (1.2.3) < ---[D]
176
- ` -- node_modules
177
- ` -- quux (3.2.0) < ---[E]
174
+ +-- asdf (2.3.4)
175
+ +-- baz (1.2.3) < ---[D]
176
+ +-- quux (3.2.0) < ---[E]
178
177
```
179
178
180
179
Since foo depends directly on ` bar@1.2.3 ` and ` baz@1.2.3 ` , those are
@@ -185,17 +184,16 @@ dependency on version 1.2.5. So, that gets installed at [A]. Since the
185
184
parent installation of blerg satisfies bar's dependency on ` blerg@1.x ` ,
186
185
it does not install another copy under [ B] .
187
186
188
- Bar [B] also has dependencies on baz and asdf, so those are installed in
189
- bar' s `node_modules` folder. Because it depends on `baz@2.x`, it cannot
187
+ Bar [ B] also has dependencies on baz and asdf. Because it depends on ` baz@2.x ` , it cannot
190
188
re-use the ` baz@1.2.3 ` installed in the parent ` node_modules ` folder [ D] ,
191
- and must install its own copy [C].
189
+ and must install its own copy [ C] . In order to minimize duplication, npm hoists
190
+ dependencies to the top level by default, so asdf is installed under [ A] .
192
191
193
192
Underneath bar, the ` baz -> quux -> bar ` dependency creates a cycle.
194
193
However, because bar is already in quux's ancestry [ B] , it does not
195
- unpack another copy of bar into that folder.
196
-
197
- Underneath ` foo -> baz` [D], quux' s [E] folder tree is empty, because its
198
- dependency on bar is satisfied by the parent folder copy installed at [B].
194
+ unpack another copy of bar into that folder. Likewise, quux's [ E]
195
+ folder tree is empty, because its dependency on bar is satisfied
196
+ by the parent folder copy installed at [ B] .
199
197
200
198
For a graphical breakdown of what is installed where, use ` npm ls ` .
201
199
0 commit comments