1

I have a HOME environment variable set in Windows to point to a folder, C:\tom\.

When I run git from the command line, and it invokes ssh, it ends up looking for the ~/.ssh folder, or $HOME/.ssh, and finds C:\tom\.ssh. This side of things works fine.

When npm runs git, on the other hand, as it might when trying to get a dependency that refers to a private github repo, ssh seems to end up trying C:\Users\tom\.ssh, aka %USERPROFILE%\.ssh. But that folder doesn't exist on my PC, so ssh fails, because it can't find the known_hosts entry for github, and ultimately git fails:

npm ERR! Command failed: git -c core.longpaths=true fetch -a origin
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.

npm config list produces the following:

; cli configs
user-agent = "npm/3.10.8 node/v6.9.1 win32 x64"

; builtin config undefined
prefix = "C:\\Users\\tom\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files (x86)\nodejs\node.exe
; cwd = C:\
; HOME = C:\Users\tom
; "npm config ls -l" to show all defaults.

So that makes sense, and presumably npm's HOME setting is coming through in the environment when it runs git. So I tried npm config set HOME C:\tom, hoping this would fix things... but it made no difference.

How do I sort this out? I want ssh always to find C:\tom\.ssh. I don't ever want it to find C:\Users\tom\.ssh. How can I make that happen? I don't mind if I have to do this by tweaking some setting, if there's some option that stops npm modifying the environment, or something else.

Tom Seddon
  • 2,648
  • 1
  • 19
  • 28
  • 1
    `npm set HOME "C:\tom" -g` (global flag) – Emissary Jun 20 '17 at 11:39
  • Thanks - that didn't fix things though. – Tom Seddon Jun 20 '17 at 11:52
  • I'm guessing this is related to the logic [here](https://github.com/npm/npm/blob/latest/node_modules/osenv/node_modules/os-homedir/index.js) - it prefers USERPROFILE to HOME, but since USERPROFILE is mandatory and HOME optional it should probably be the other way round. – Tom Seddon Jun 20 '17 at 12:00
  • hmm okay, is it package scripts that contain these git commands - have you tried [overriding the environment variables](https://stackoverflow.com/questions/37140799/passing-environment-variables-in-npm-scripts) before the command. Sorry not sure what else to suggest. – Emissary Jun 20 '17 at 12:10
  • 1
    No scripts... just dependencies in package.json that refer to private GitHub repos. What I've done for now is make a junction from C:\Users\tom\.ssh to C:\tom.ssh, which I can tolerate. [I've also opened a GitHub issue](https://github.com/nodejs/node/issues/13818). – Tom Seddon Jun 20 '17 at 12:20
  • Since the original issue was closed, I opened a new issue: https://github.com/nodejs/node/issues/37431 – Derek Greer Feb 18 '21 at 13:21

1 Answers1

0

As you discovered, npm sets $HOME to $USERPROFILE on Windows systems.

One approach to making npm use the correct value for $HOME when executed using bash on Windows is to set USERPROFILE to the value of $HOME:

export USERPROFILE=$HOME
Derek Greer
  • 15,454
  • 5
  • 45
  • 52