<h1 dir="auto">What / Why</h1>

<p dir="auto">npm: v5.8.0, v6.13.4</p>
<p dir="auto">When I attempt to install a package from GitHub globally, I am getting a permission error: "could not create leading directories".</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ sudo -H npm install -g git+https://github.com/richRemer/pool.git
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
npm ERR! 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-29T20_06_58_093Z-debug.log"><pre class="notranslate"><code class="notranslate">$ sudo -H npm install -g git+https://github.com/richRemer/pool.git
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
npm ERR! 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-29T20_06_58_093Z-debug.log
</code></pre></div>
<p dir="auto">The relevant log bits:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="12 silly fetchPackageMetaData error for git+https://github.com/richRemer/pool.git Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
12 silly fetchPackageMetaData fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
13 verbose stack Error: Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
13 verbose stack fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
13 verbose stack
13 verbose stack     at ChildProcess.exithandler (child_process.js:294:12)
13 verbose stack     at ChildProcess.emit (events.js:189:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Socket.stream.socket.on (internal/child_process.js:389:11)
13 verbose stack     at Socket.emit (events.js:189:13)
13 verbose stack     at Pipe._handle.close (net.js:600:12)
14 verbose cwd /home/rremer
15 verbose Linux 5.3.0-24-generic
16 verbose argv &quot;/usr/bin/node&quot; &quot;/usr/bin/npm&quot; &quot;install&quot; &quot;-g&quot; &quot;git+https://github.com/richRemer/pool.git&quot;
17 verbose node v10.15.2
18 verbose npm  v5.8.0
19 error code 128
20 error Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
20 error fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
21 verbose exit [ 1, true ]"><pre class="notranslate"><code class="notranslate">12 silly fetchPackageMetaData error for git+https://github.com/richRemer/pool.git Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
12 silly fetchPackageMetaData fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
13 verbose stack Error: Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
13 verbose stack fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
13 verbose stack
13 verbose stack     at ChildProcess.exithandler (child_process.js:294:12)
13 verbose stack     at ChildProcess.emit (events.js:189:13)
13 verbose stack     at maybeClose (internal/child_process.js:970:16)
13 verbose stack     at Socket.stream.socket.on (internal/child_process.js:389:11)
13 verbose stack     at Socket.emit (events.js:189:13)
13 verbose stack     at Pipe._handle.close (net.js:600:12)
14 verbose cwd /home/rremer
15 verbose Linux 5.3.0-24-generic
16 verbose argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "git+https://github.com/richRemer/pool.git"
17 verbose node v10.15.2
18 verbose npm  v5.8.0
19 error code 128
20 error Command failed: /usr/bin/git clone -q https://github.com/richRemer/pool.git /root/.npm/_cacache/tmp/git-clone-e33b6b4f
20 error fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-e33b6b4f': Permission denied
21 verbose exit [ 1, true ]
</code></pre></div>
<h2 dir="auto">When</h2>
<ul dir="auto">
<li>happens when using "... install -g git+https://..."</li>
<li>happens with all repos, public or private, but ultimately I'm trying to solve this for private repos</li>
<li>happens whether I use "sudo -H" or if switch to root user and run directly</li>
</ul>
<h2 dir="auto">Where</h2>
<ul dir="auto">
<li>n/a</li>
</ul>
<h2 dir="auto">How</h2>
<h3 dir="auto">Current Behavior</h3>
<ul dir="auto">
<li>fails to install global packages from GitHub</li>
</ul>
<h3 dir="auto">Steps to Reproduce</h3>
<ul dir="auto">
<li>run the following command</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="sudo -H npm install -g git+https://github.com/richRemer/pool.git"><pre class="notranslate"><code class="notranslate">sudo -H npm install -g git+https://github.com/richRemer/pool.git
</code></pre></div>
<h3 dir="auto">Expected Behavior</h3>
<ul dir="auto">
<li>it should work the same as running:</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="git clone https://github.com/richRemer/pool.git
sudo -H npm install -g $(npm pack pool/)"><pre class="notranslate"><code class="notranslate">git clone https://github.com/richRemer/pool.git
sudo -H npm install -g $(npm pack pool/)
</code></pre></div>
<h2 dir="auto">Who</h2>
<ul dir="auto">
<li>n/a</li>
</ul>
<h2 dir="auto">References</h2>
<ul dir="auto">
<li><a href="https://docs.npmjs.com/cli/install" rel="nofollow">https://docs.npmjs.com/cli/install</a> (git+https syntax)</li>
<li><a href="https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users#comment-58129097" rel="nofollow">https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users#comment-58129097</a> (using nvm is a non-starter)</li>
</ul>
<h1 dir="auto">What / Why</h1>
<p dir="auto">This bug is related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="521884660" data-permission-text="Title is private" data-url="https://github.com/npm/cli/issues/476" data-hovercard-type="issue" data-hovercard-url="/npm/cli/issues/476/hovercard?comment_id=553917541&amp;comment_type=issue_comment" href="https://github.com/npm/cli/issues/476#issuecomment-553917541">#476 (comment)</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="527439839" data-permission-text="Title is private" data-url="https://github.com/npm/cli/issues/514" data-hovercard-type="issue" data-hovercard-url="/npm/cli/issues/514/hovercard" href="https://github.com/npm/cli/issues/514">#514</a></p>
<h2 dir="auto">When</h2>
<p dir="auto"><code class="notranslate">npm install</code> packages from git in docker</p>
<h2 dir="auto">Where</h2>
<ul dir="auto">
<li>packages from public git repositories</li>
</ul>
<h2 dir="auto">How</h2>
<h3 dir="auto">Current Behavior</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# npm i
npm ERR! code 128
npm ERR! Command failed: git clone --depth=1 -q -b v2.0.5 git://github.com/boblauer/MockDate.git /root/.npm/_cacache/tmp/git-clone-c9699b7f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-c9699b7f': Permission denied
npm ERR! 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-23T09_49_05_990Z-debug.log"><pre class="notranslate"><code class="notranslate"># npm i
npm ERR! code 128
npm ERR! Command failed: git clone --depth=1 -q -b v2.0.5 git://github.com/boblauer/MockDate.git /root/.npm/_cacache/tmp/git-clone-c9699b7f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-c9699b7f': Permission denied
npm ERR! 

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-23T09_49_05_990Z-debug.log
</code></pre></div>
<h3 dir="auto">Steps to Reproduce</h3>
<ul dir="auto">
<li>Create simple package.json with git dependency</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;name&quot;: &quot;test-npm&quot;,
  &quot;dependencies&quot;: {
    &quot;mockdate&quot;: &quot;git://github.com/boblauer/MockDate.git#v2.0.5&quot;
  }
}"><pre class="notranslate"><code class="notranslate">{
  "name": "test-npm",
  "dependencies": {
    "mockdate": "git://github.com/boblauer/MockDate.git#v2.0.5"
  }
}
</code></pre></div>
<ul dir="auto">
<li>run <code class="notranslate">docker run --network=host -it -v $(pwd):/var/work/test -w /var/work/test node:12.14.0 /bin/bash</code></li>
<li><code class="notranslate">ls -l</code> shows that files are owned by unknown user and unknown group (this is typical behaviour on build agents like teamcity, teamcity clones git repo and mounts it inside docker container)</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# ls -l
-rw-r--r-- 1 20857 115755  114 Dec 23 09:46 package.json"><pre class="notranslate"><code class="notranslate"># ls -l
-rw-r--r-- 1 20857 115755  114 Dec 23 09:46 package.json
</code></pre></div>
<ul dir="auto">
<li>run <code class="notranslate">npm install</code></li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# Depends on environment you can get

npm ERR! code 128
npm ERR! Command failed: git clone --depth=1 -q -b v2.0.5 git://github.com/boblauer/MockDate.git /root/.npm/_cacache/tmp/git-clone-c9699b7f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-c9699b7f': Permission denied
npm ERR! 

# or

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@&lt;private server&gt;/admin-template.git
npm ERR! 
npm ERR! No user exists for uid 1000
npm ERR! fatal: Could not read from remote repository."><pre class="notranslate"><code class="notranslate"># Depends on environment you can get

npm ERR! code 128
npm ERR! Command failed: git clone --depth=1 -q -b v2.0.5 git://github.com/boblauer/MockDate.git /root/.npm/_cacache/tmp/git-clone-c9699b7f
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-c9699b7f': Permission denied
npm ERR! 

# or

npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git@&lt;private server&gt;/admin-template.git
npm ERR! 
npm ERR! No user exists for uid 1000
npm ERR! fatal: Could not read from remote repository.
</code></pre></div>
<ul dir="auto">
<li>run <code class="notranslate">npm i -g npm@6.10</code> (downgrade to npm 6.10.X)</li>
<li>run <code class="notranslate">npm install</code></li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# all works well!

updated 1 package and audited 1 package in 1.355s
found 0 vulnerabilities"><pre class="notranslate"><code class="notranslate"># all works well!

updated 1 package and audited 1 package in 1.355s
found 0 vulnerabilities
</code></pre></div>
<h3 dir="auto">Expected Behavior</h3>
<ul dir="auto">
<li><code class="notranslate">npm install</code> / <code class="notranslate">npm ci</code> works well like npm@6.10</li>
</ul>