<h3 dir="auto">Current Behavior:</h3>
<p dir="auto">When the dependency tree contains many versions for one subdependency, npm puts the highest version at the top level (i.e., at node_modules/pkgname), even when another version has more referrals. This other version is then installed many times.</p>
<h3 dir="auto">Expected Behavior:</h3>
<p dir="auto">NPM checks for each package (that is not a direct dependency of the project) which version appears most often and puts that one on the top level, reducing duplication.</p>
<h3 dir="auto">Steps To Reproduce:</h3>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="npm init
npm i next@10.0.2
npm ls source-map"><pre class="notranslate">npm init
npm i next@10.0.2
npm ls source-map</pre></div>
<p dir="auto">source-map@0.6.1 should be the top level one, not @0.7.3.</p>
<h3 dir="auto">Environment:</h3>
<p dir="auto">Windows 10<br>
Node 14.13.0<br>
Npm 7.0.12</p>
<h3 dir="auto">Current Behaviour:</h3>
<p dir="auto">I have some npm packages installed globally, which are command line utilities (<code class="notranslate">create-react-app</code>, <code class="notranslate">distributed-dig</code>, snyk`, and others). I recently updated to npm version 7 (7.5.2) with node 14 (14.15.4) on Windows 10 (version: 2004 || build: 19041.746).</p>
<p dir="auto">If I perform a global <code class="notranslate">dedupe</code> ...</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="npm dedupe -g"><pre class="notranslate">npm dedupe -g</pre></div>
<p dir="auto">... a number of the globally installed packages are removed. Curiously not all of them. In the example below eight of the 11 are removed.</p>
<p dir="auto">Incidentally, I <strong>couldn't</strong> reproduce this behaviour with npm v7.5.2 &amp; node v15.5.1 on <a href="https://termbin.com/o044" rel="nofollow">Linux Mint v19</a></p>
<h3 dir="auto">Expected Behaviour:</h3>
<p dir="auto">I'd expect the same behaviour as npm v6 where each top-level globally installed package persists after the global dedupe. In the example steps below I'd expect all 11 globally installed packages to still be installed after the dedupe.</p>
<h3 dir="auto">Steps To Reproduce:</h3>
<p dir="auto">Report current versions: <code class="notranslate">npm -v ; node -v</code></p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="PS C:\Users&gt; npm -v;node -v
7.5.2
v14.15.4"><pre class="notranslate">PS C:<span class="pl-cce">\U</span>sers<span class="pl-k">&gt;</span> npm -v<span class="pl-k">;</span>node -v
7.5.2
v14.15.4</pre></div>
<p dir="auto">List currently installed global packages (and report a count): <code class="notranslate">npm ls -g --depth 0;npm ls -g --depth 0|find </code>"--<code class="notranslate">" /c</code></p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="PS C:\Users&gt; npm ls -g --depth 0;npm ls -g --depth 0|find `&quot;--`&quot; /c
C:\Users\markm\AppData\Roaming\npm
+-- akamai-error-lookup@1.2.8
+-- akamai-staging@1.1.8
+-- ascii-art@2.5.0
+-- cdn-cache-check@1.6.0
+-- codename-generator@1.0.15
+-- create-react-app@4.0.2
+-- depcheck@1.3.1
+-- distributed-dig@1.8.3
+-- eslint@7.19.0
+-- jshint@2.12.0
+-- npm@7.5.2
`-- snyk@1.441.0

12"><pre class="notranslate">PS C:<span class="pl-cce">\U</span>sers<span class="pl-k">&gt;</span> npm ls -g --depth 0<span class="pl-k">;</span>npm ls -g --depth 0<span class="pl-k">|</span>find <span class="pl-s"><span class="pl-pds">`</span><span class="pl-s"><span class="pl-pds">"</span>--<span class="pl-s"><span class="pl-pds">`</span><span class="pl-s"><span class="pl-pds">"</span> /c</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">C:\Users\markm\AppData\Roaming\npm</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- akamai-error-lookup@1.2.8</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- akamai-staging@1.1.8</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- ascii-art@2.5.0</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- cdn-cache-check@1.6.0</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- codename-generator@1.0.15</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- create-react-app@4.0.2</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- depcheck@1.3.1</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- distributed-dig@1.8.3</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- eslint@7.19.0</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- jshint@2.12.0</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- npm@7.5.2</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-pds">`</span>-- snyk@1.441.0</span></span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"></span></span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">12</span></span></span></span></span></pre></div>
<p dir="auto">Perform a global dedupe: <code class="notranslate">npm dedupe -g</code></p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="PS C:\Users&gt; npm dedupe -g

added 1 package, removed 1334 packages, and audited 256 packages in 6s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities"><pre class="notranslate">PS C:<span class="pl-cce">\U</span>sers<span class="pl-k">&gt;</span> npm dedupe -g

added 1 package, removed 1334 packages, and audited 256 packages <span class="pl-k">in</span> 6s

11 packages are looking <span class="pl-k">for</span> funding
  run <span class="pl-s"><span class="pl-pds">`</span>npm fund<span class="pl-pds">`</span></span> <span class="pl-k">for</span> details

found 0 vulnerabilities</pre></div>
<p dir="auto"><em>Notice that</em> <code class="notranslate">removed 1334 packages</code></p>
<p dir="auto">Re-list globally installed packages (and a new count): <code class="notranslate">npm ls -g --depth 0;npm ls -g --depth 0|find </code>"--<code class="notranslate">" /c</code></p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="PS C:\Users&gt; npm ls -g --depth 0;npm ls -g --depth 0|find `&quot;--`&quot; /c
C:\Users\markm\AppData\Roaming\npm
+-- acorn-jsx@5.3.1
+-- npm@7.5.2
`-- prettyjson@1.2.1

3"><pre class="notranslate">PS C:<span class="pl-cce">\U</span>sers<span class="pl-k">&gt;</span> npm ls -g --depth 0<span class="pl-k">;</span>npm ls -g --depth 0<span class="pl-k">|</span>find <span class="pl-s"><span class="pl-pds">`</span><span class="pl-s"><span class="pl-pds">"</span>--<span class="pl-s"><span class="pl-pds">`</span><span class="pl-s"><span class="pl-pds">"</span> /c</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">C:\Users\markm\AppData\Roaming\npm</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- acorn-jsx@5.3.1</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">+-- npm@7.5.2</span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-pds">`</span>-- prettyjson@1.2.1</span></span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"></span></span></span></span></span>
<span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s"><span class="pl-s">3</span></span></span></span></span></pre></div>
<p dir="auto">Eight of the the initial 11 global packages are now removed<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9842107/106898125-b41bca80-66eb-11eb-9eaf-8ba8937c6409.gif"><img src="https://user-images.githubusercontent.com/9842107/106898125-b41bca80-66eb-11eb-9eaf-8ba8937c6409.gif" alt="2021-02-04_12h19_39" data-animated-image="" style="max-width: 100%;"></a></p>
<h3 dir="auto">Environment:</h3>
<ul dir="auto">
<li>OS: Windows 10 (version: 2004 || build: 19041.746)</li>
<li>Node: 14.15.4</li>
<li>npm: 7.5.2</li>
<li>PowerShell 7.1.1 (although I still see this behaviour when using <code class="notranslate">cmd.exe</code> instead:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9842107/106899176-f396e680-66ec-11eb-9ce0-d2f0f2c0fd34.gif"><img src="https://user-images.githubusercontent.com/9842107/106899176-f396e680-66ec-11eb-9ce0-d2f0f2c0fd34.gif" alt="2021-02-04_13h27_40" data-animated-image="" style="max-width: 100%;"></a></li>
</ul>