<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">Current Behavior</h3>
<p dir="auto"><code class="notranslate">npm install -g</code> creates a folder with the literal name <code class="notranslate">${XDG_DATA_HOME}</code> in the current directory, and treats it as the data home dir.</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/248078/127122075-f49be28e-e4b2-4af8-814b-d392f915c826.png"><img width="210" alt="127120221-54e6ec3d-4a4c-4cd7-ac98-12b7f6f40c12" src="https://user-images.githubusercontent.com/248078/127122075-f49be28e-e4b2-4af8-814b-d392f915c826.png" style="max-width: 100%;"></a></p>
<p dir="auto">Presumably something in the code is not escaping the variable properly under some conditions.</p>
<p dir="auto">Note that I do have <code class="notranslate">$XDG_HOME_DIR</code> <a href="https://github.com/lgarron/dotfiles/blob/720ee35b2ea2655e31bf480c7e803958e88e6b39/dotfiles/xdg-basedir-workarounds/.config/fish/xdg-basedir-workarounds.fish#L11">in my dotfiles</a> and <a href="https://github.com/lgarron/dotfiles/blob/720ee35b2ea2655e31bf480c7e803958e88e6b39/dotfiles/mac-boot/.config/boot.fish#L12">using <code class="notranslate">launchctl</code></a>. I have not been able to avoid the bug by un-setting the var.</p>
<p dir="auto">This did not use to happen until the last few days. Unfortunately, this is rather annoying to debug because the bug itself specifically breaks global <code class="notranslate">npm</code> installation. So I can't run <code class="notranslate">npm install -g npm@7.0.0</code> or <code class="notranslate">npm install -g npm@7.20.1</code> to get a proper install of another version, for example.</p>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">The command uses the value of the <code class="notranslate">XDG_DATA_HOME</code> env var (or the default), without creating a dir named <code class="notranslate">${XDG_DATA_HOME}</code>.</p>
<h3 dir="auto">Steps To Reproduce</h3>
<p dir="auto">Run <code class="notranslate">npm install -g</code> or <code class="notranslate">npx</code>. For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="npm install -g jq"><pre class="notranslate"><code class="notranslate">npm install -g jq
</code></pre></div>
<p dir="auto">Running <code class="notranslate">npx jq</code> also gives this error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="node:internal/modules/cjs/loader:930
  throw err;
  ^

Error: Cannot find module 'commander'
Require stack:
- /Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
    at Function.Module._load (node:internal/modules/cjs/loader:772:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.&lt;anonymous&gt; (/Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq:7:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq'
  ]
}"><pre class="notranslate"><code class="notranslate">node:internal/modules/cjs/loader:930
  throw err;
  ^

Error: Cannot find module 'commander'
Require stack:
- /Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
    at Function.Module._load (node:internal/modules/cjs/loader:772:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.&lt;anonymous&gt; (/Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq:7:15)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/lgarron/.cache/npm/_npx/db665b9ed68ddd8b/node_modules/jq/bin/jq'
  ]
}
</code></pre></div>
<p dir="auto">I don't know how related it is, but it's what led me to observing the directory issue.</p>
<h3 dir="auto">Environment</h3>
<p dir="auto">npm 7.19.1<br>
node v16.5.0<br>
macOS 11.5</p>
<p dir="auto">I know this report is not super helpful, but I could use some advice on how to debug this.<br>
I've spent an hour in <code class="notranslate">@npmcli/config</code> to figure out where this is coming from, but it uses a lot of indirection to load configs. Most useful would probably be to bisect some packages, but I don't know how to do that when <code class="notranslate">npm install -g</code> is not working. (I tried to symlink <code class="notranslate">./${XDG_DATA_HOME}</code> to the appropriate global directory, but that doesn't seem to work.)</p>
<h3 dir="auto">Current Behavior:</h3>

<p dir="auto"><code class="notranslate">npm uninstall -g &lt;package&gt;</code> doesn't run the <code class="notranslate">preuninstall</code> script</p>
<p dir="auto">Also, <code class="notranslate">npm install -g &lt;package&gt;</code> doesn't run the <code class="notranslate">preuninstall</code> script from the previously installed version of the same package</p>
<p dir="auto">Installing a newer version of a global package does trigger <code class="notranslate">preuninstall</code> in v6</p>
<h3 dir="auto">Expected Behavior:</h3>

<p dir="auto"><code class="notranslate">npm uninstall -g &lt;package&gt;</code> should run the <code class="notranslate">preuninstall</code> script</p>
<h3 dir="auto">Steps To Reproduce:</h3>
<p dir="auto">Since npm7 removed console output for scripts during installation, so I echo something to a logfile instead.</p>
<ol dir="auto">
<li>Create a package with these scripts:</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;name&quot;: &quot;testpackage&quot;,
  &quot;version&quot;:&quot;0.0.1&quot;,
  &quot;scripts&quot;: {
    &quot;preinstall&quot;: &quot;echo \&quot;preinstall\&quot; &gt;&gt; /logs/log.txt&quot;,
    &quot;preuninstall&quot;: &quot;echo \&quot;preuninstall\&quot; &gt;&gt; /logs/log.txt&quot;
  }
}"><pre class="notranslate"><code class="notranslate">{
  "name": "testpackage",
  "version":"0.0.1",
  "scripts": {
    "preinstall": "echo \"preinstall\" &gt;&gt; /logs/log.txt",
    "preuninstall": "echo \"preuninstall\" &gt;&gt; /logs/log.txt"
  }
}
</code></pre></div>
<ol start="2" dir="auto">
<li>run <code class="notranslate">npm pack</code></li>
<li>run <code class="notranslate">npm install -g ./testpackage-0.0.1.tgz</code></li>
<li>run <code class="notranslate">npm install -g ./testpackage-0.0.1.tgz</code> again (this should have triggered <code class="notranslate">preuninstall</code> for the previous installation)</li>
<li>run <code class="notranslate">npm uninstall -g testpackage</code></li>
<li>confirm that the text file in /logs/log.txt does in fact not contain any 'preuninstall' lines</li>
</ol>
<p dir="auto">Images:<br>
In v7:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/8776771/113917286-51c17200-97e1-11eb-9173-60e0671dfd7b.png"><img src="https://user-images.githubusercontent.com/8776771/113917286-51c17200-97e1-11eb-9173-60e0671dfd7b.png" alt="Code_kIGgtizrAQ" style="max-width: 100%;"></a><br>
In v6:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/8776771/113917331-600f8e00-97e1-11eb-9f68-fa55fc877c41.png"><img src="https://user-images.githubusercontent.com/8776771/113917331-600f8e00-97e1-11eb-9f68-fa55fc877c41.png" alt="Code_RSlxOsyZwd" style="max-width: 100%;"></a></p>
<h3 dir="auto">Environment:</h3>
<ul dir="auto">
<li>OS: Windows 10</li>
<li>Node: 15.14.0</li>
<li>npm: 7.7.6</li>
</ul>