<p dir="auto">When I try to upgrade npm via the suggested method in the console:</p>
<p dir="auto"><code class="notranslate">npm install -g npm@8.3.0</code></p>
<p dir="auto">it fails with the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -&gt; '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -&gt; '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/npm',
npm ERR!   dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR! }
npm ERR! "><pre class="notranslate"><code class="notranslate">npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -&gt; '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -&gt; '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/npm',
npm ERR!   dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR! }
npm ERR! 
</code></pre></div>
<p dir="auto">When I try to run it with <code class="notranslate">sudo</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="npm ERR! code EPERM
npm ERR! syscall lchown
npm ERR! path /usr/local/bin/sentinelctl
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'
npm ERR!  [Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'] {
npm ERR!   errno: -1,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'lchown',
npm ERR!   path: '/usr/local/bin/sentinelctl'
npm ERR! }"><pre class="notranslate"><code class="notranslate">npm ERR! code EPERM
npm ERR! syscall lchown
npm ERR! path /usr/local/bin/sentinelctl
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'
npm ERR!  [Error: EPERM: operation not permitted, lchown '/usr/local/bin/sentinelctl'] {
npm ERR!   errno: -1,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'lchown',
npm ERR!   path: '/usr/local/bin/sentinelctl'
npm ERR! }
</code></pre></div>
<p dir="auto"><code class="notranslate">sentinelctl</code> is control panel for Sentinel One antivirus, I guess its one of their security measures to make it not chown-able.</p>
<p dir="auto">OS: OSX 11.6.1</p>
<p dir="auto">npm --version<br>
7.20.3</p>
<h3 dir="auto">Is there an existing issue for this?</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the existing issues</li>
</ul>
<h3 dir="auto">This issue exists in the latest npm version</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I am using the latest npm</li>
</ul>
<h3 dir="auto">Current Behavior</h3>
<p dir="auto">After running <code class="notranslate">sudo npm i -g foo</code> files under <code class="notranslate">/usr/bin</code> that were owned by <code class="notranslate">root</code> are now owned by the current user.</p>
<p dir="auto">I ran <code class="notranslate">sudo npm i -g typescript</code> and it was installed correctly I think. Then I ran <code class="notranslate">sudo npm i -g vscode-langservers-extracted</code> and I got <code class="notranslate">sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set</code>.</p>
<p dir="auto">I think the packages I installed are irrelevant because I still get the same errors after installing other packages globally with sudo.</p>
<p dir="auto">This looks similar to #19883 but as far as I can see, only <code class="notranslate">/usr/bin/</code> files are affected.</p>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">npm should not change ownership of files under <code class="notranslate">/usr/bin</code></p>
<h3 dir="auto">Steps To Reproduce</h3>
<ol dir="auto">
<li>In this environment...<br>
Linux<br>
npm v8.1.3</li>
<li>With this config...<br>
Default</li>
<li>Run 'sudo npm i -g typescript' (the package doesn't matter)</li>
<li>See: files under /usr/bin changing ownership</li>
</ol>
<p dir="auto">In this video, you can see what happens. When I stop writing is because I'm going to another tty as root to execute <code class="notranslate">chown root:root /usr/bin/sudo &amp;&amp; chmod 4755 /usr/bin/sudo</code>. The video also shows npm with version <code class="notranslate">v8.0.0</code> but I updated to <code class="notranslate">v8.1.3</code> and the same thing happens.</p>
<details open="" class="details-reset border rounded-2">
  <summary class="px-3 py-2">
    <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-device-camera-video">
    <path d="M16 3.75v8.5a.75.75 0 0 1-1.136.643L11 10.575v.675A1.75 1.75 0 0 1 9.25 13h-7.5A1.75 1.75 0 0 1 0 11.25v-6.5C0 3.784.784 3 1.75 3h7.5c.966 0 1.75.784 1.75 1.75v.675l3.864-2.318A.75.75 0 0 1 16 3.75Zm-6.5 1a.25.25 0 0 0-.25-.25h-7.5a.25.25 0 0 0-.25.25v6.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-6.5ZM11 8.825l3.5 2.1v-5.85l-3.5 2.1Z"></path>
</svg>
    <span aria-label="Video description 2021-11-07.14-39-43.mov" class="m-1">2021-11-07.14-39-43.mov</span>
    <span class="dropdown-caret"></span>
  </summary>

  <video src="https://user-images.githubusercontent.com/78516649/140661644-b54fa9ce-7ce1-43de-a1c5-2bd892ce29e6.mov" data-canonical-src="https://user-images.githubusercontent.com/78516649/140661644-b54fa9ce-7ce1-43de-a1c5-2bd892ce29e6.mov" controls="controls" muted="muted" class="d-block rounded-bottom-2 border-top width-fit" style="max-height:640px; min-height: 200px">

  </video>
</details>

<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>npm: 8.1.3</li>
<li>Node: 16.11.0</li>
<li>OS: Linux 5.10.70-1-MANJARO</li>
<li>platform: Desktop amd64</li>
<li>npm config:</li>
</ul>
<div class="highlight highlight-source-ini notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="; node bin location = /usr/bin/node
; cwd = /home/emi2k01/Documents/facu/tecmul/proyectofinal
; HOME = /home/emi2k01
; Run `npm config ls -l` to show all defaults."><pre class="notranslate"><span class="pl-c"><span class="pl-c">;</span> node bin location = /usr/bin/node</span>
<span class="pl-c"><span class="pl-c">;</span> cwd = /home/emi2k01/Documents/facu/tecmul/proyectofinal</span>
<span class="pl-c"><span class="pl-c">;</span> HOME = /home/emi2k01</span>
<span class="pl-c"><span class="pl-c">;</span> Run `npm config ls -l` to show all defaults.</span></pre></div>