25

Getting permission denied error for a local repo in my company github, which I can clone successfully, however getting error for NPM install:

npm ERR! Error while executing:
npm ERR! C:\Program Files\Git\bin\git.EXE ls-remote -h -t git@<repo path and name>
npm ERR!
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
rdarioduarte
  • 1,929
  • 2
  • 21
  • 29
Nik
  • 351
  • 1
  • 3
  • 3

6 Answers6

20

I ran into this problem and was not able to fix as all the solutions lacked one important step, so I am posting my answer in greater detail.

Assuming you have already generated ssh-key and added its public key to your github account. Also, assuming you are using Windows-10.

npm install on Windows causes a lot of problems because there are ssh-agents running from 2 different sources:

  • Windows provided ssh-agent (OpenSSH). This feature was added in one of the Windows-10 updates and is the culprit. :)
  • Git ssh-agent (this one is required for your scenario)(assuming git is installed.)

Detect source of running ssh-agent

  • Run start-ssh-agent (to run git provided ssh-agent) and ssh-agent (to run Windows provided ssh-agent) in cmd.

  • Go to task manager and click on details tab.

  • Look for ssh-agent.exe.

After following above steps, you can see 2 ssh-agent.exe running. In the 'User name' column you can see one for SYSTEM (this is provided by Windows) and one for your-user-name (this is git). Now, please close all ssh-agents you started for npm install(directly from task manager).

Cause of issue:

ssh-agent that npm install is trying to use is from the git but it is being conflicted with the windows provided ssh-agent.

Git ssh-agent can be started using start-ssh-agent but when you try to add ssh-key to your ssh-agent using command ssh-add -k ~/.ssh/id_rsa, it is trying to access ssh-add.exe from Windows path but not picking ssh-add.exe from git.

Solution:

Solution is to add ssh path of git to the Environment variables of your system and adding it before the "%SYSTEMROOT%\System32\OpenSSH" so it has higher priority.

This is only required so that you can access ssh-add.exe of git and after successful npm build you can freely remove it(even if you want to clone more repos and build them). Unless you want to add more keys for different repos.

Note: This path is different from the already present git/cmd path added during git installation so please make sure it is added before openSSh path variable for higher priority.

Please follow below steps to resolve your issue:

  1. Start your git ssh-agent by running start-ssh-agent in cmd. Don't use ssh-agent as it starts the Windows provided ssh-agent.
  2. Open Task Manager and go to Details tab.
  3. Look for ssh-agent.exe.
  4. Right click on ssh-agent.exe(on the one provided by git) and click open file location.
  5. Copy the path and add it to Environment variables for system. Please make sure you move this path above "%SYSTEMROOT%\System32\OpenSSH".(Ignore if already present).
  6. Close cmd and also close the running ssh-agent.exe from task manager. Note: Closing cmd is required for Env. variables to take effect.
  7. Open cmd in your repo and run where ssh-agent and where ssh-add to test which ssh path will be preferred. (Hope you see 2 paths now and OpenSSH should not be appearing before Git/bin/usr path :) )
  8. Now, you are done. Run start-ssh-agent.(Runs git ssh-agent)
  9. Add your preferred ssh key to you ssh-agent by ssh-add -k ~/.ssh/id_rsa.(choose private key). (Adds keys to git ssh-agent, not Windows ssh-agent)
  10. Once successfully added, run npm install.

Note: After this, whenever you see the same error again, just run start-ssh-agent and then npm install.

I hope it works, and if you are not comfortable, after successful npm build you can even remove the newly added path from env. variables as it was required only once(to access ssh-add from git), and start-ssh-agent is not accessed from that path.

Edit: It won't affect if you delete the path but I would recommend not to delete the path as this path also has many other useful tools like perl.exe, openssl.exe (don't need to install these tools separately).

Sanidhya Gaur
  • 301
  • 3
  • 7
  • 3
    You can also use the system SSH. Enable and start the "OpenSSH Authentication Agent" service. https://stackoverflow.com/a/52113739/991267 Then update the Git config: git config --global core.sshCommand "'C:\Windows\System32\OpenSSH\ssh.exe'" https://github.com/desktop/desktop/issues/5641#issuecomment-421801704 – Der_Meister May 03 '21 at 17:05
  • @Der_Meister you should add this as an answer – Holistic Developer Oct 07 '21 at 22:37
9

Are you saying your lib is published to the npm registry or are you trying to use npm to install directly from github? It sounds like you're trying to do npm install company-org/repo-name. If that's so then you're more than likely trying to get npm to pull from a private repo. You can clone because you've probably setup an SSH key for git to use to authenticate with Github.

Double check that SSH is setup and then use this syntax in package.json:

"package-name": "git+ssh://git@github.com:<company-org>/<repo>.git"

(note the use of colon instead of slash before company-org)

CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • I got following error for ssh -T edl@.com Permission denied (publickey). – Nik Sep 19 '17 at 18:18
  • 1
    @Nik did you make sure that `ssh-agent` is running? [Adding your SSH key to the ssh-agent](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agent) – zero298 Sep 19 '17 at 18:57
1

If you're using Windows, try Git Bash instead of CMD to run the npm install

0

If you are trying to install any dependency globally/locally make sure to use @ in the start. For example: sudo npm i -g @vue/cli --correct. sudo npm i -g vue/cli -- throws above mentioned error.

0

I have also faced a similar issue. Here is the Step By Step Procedure to solve this issue as below:

Step 1: Check if SSH Key already exists, run the below command,

$ cd ~/.ssh

$ ls

If you don't see id_rsa.pub, use the following command to generate a new key pair.

Step 2: Generating a new SSH Key, run the below command, (Keep pressing enter for setting all the default values)

$ ssh-keygen -o -t rsa -C "abc@email.com"

Step 3: Add your public key to GitHub

$ cat ~/.ssh/id_rsa.pub

Copy the output displayed on your terminal.

Step 4: Log in to github.com and follow the steps:

  • Go to "settings" -> "SSH and GPG Keys"
  • Click on the button "Add SSH Key"
  • Add title and paste the key in the key section.

Step 5: Configure SSO (Important step to complete the procedure and fix the issue)

  • Click on the button "Configure SSO" in the SSH key (Which is recently added)
  • Click on the "Authenticate" button and authenticate your account by entering credentials.

Step 6: Now if you are facing an issue something like "sign_and_send_pubkey: signing failed" then you need to add the SSH key to the agent. Run below command on the client machine (your local),

ssh-add 
Jitendra
  • 3,135
  • 2
  • 26
  • 42
0

For what it's worth and if it could help anyone in the future, I just faced this same issue because I use workspaces and mistyped the install command.

Instead of npm i ... --workspace=my_path/to_workspace I typed npm i ... --workspaces=my_path/to_workspace.

"workspaces" instead of the expected "workspace" singular.

David Dal Busco
  • 7,975
  • 15
  • 55
  • 96