@@ -128,8 +128,8 @@ Consider the case above, where `foo -> bar -> baz`. Imagine if, in
128128addition to that, baz depended on bar, so you'd have:
129129` foo -> bar -> baz -> bar -> baz ... ` . However, since the folder
130130structure 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
133133` foo/node_modules/bar ` .
134134
135135This shortcut is only used if the exact same
@@ -140,7 +140,8 @@ exact same package multiple times, an infinite regress will always be
140140prevented.
141141
142142Another 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.
144145
145146#### Example
146147
@@ -160,21 +161,19 @@ foo
160161 ` -- bar
161162```
162163
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):
164166
165167``` bash
166168foo
167169+-- node_modules
168170 +-- blerg (1.2.5) < ---[A]
169171 +-- bar (1.2.3) < ---[B]
170- | ` -- node_modules
172+ | + -- node_modules
171173 | +-- 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]
178177```
179178
180179Since 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
185184parent installation of blerg satisfies bar's dependency on ` blerg@1.x ` ,
186185it does not install another copy under [ B] .
187186
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
190188re-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] .
192191
193192Underneath bar, the ` baz -> quux -> bar ` dependency creates a cycle.
194193However, 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] .
199197
200198For a graphical breakdown of what is installed where, use ` npm ls ` .
201199
0 commit comments