npm install private Gitlab Repo Permission Denied (publickey) in Powershell

I try to install a private gitlab repository via npm to another node project. The command is npm install --save gitlab:my-project#master This fails with

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.

I have the following set up: Windows 10, git and Powershell.

Using git commands directly in Powershell is working fine, e.g. git pull asks for the password of my private key and it works. All the other git stuff is working fine except for the npm install command, which seems to use git internally.

If I use the git bash, that is installed with git on Windows, I can run the npm command and install the private repository. It only fails within Powershell.

Nevertheless one could say: use git bash, but I'm somehow used to the PowerShell. :)


Solution 1:

I had the same problem. This is how I solved it / my workaround:

  • Make sure that you can authenticate against GitHub: ssh -T [email protected]
  • Make sure OpenSSH Authentication Service is not disabled (set it to manual start) - not sure if this step is necessary
  • Now the most important step: remove the password protection from your id_rsa key. npm install can't handle password protected ssh keys.

I would recommend adding a read only SSH key directly to the repository you like to install. This makes having a SSH key without password protection a little bit less dangerous.

Add a config file into ~/.ssh/ in which you define your keys:

IdentityFile ~/.ssh/id_rsa IdentityFile ~/.ssh/repository_ssh_key

Solution 2:

Permission denied (publickey).

This means that your current public key cannot be authenticated by the remote server.


Check access via SSH

Run:

ssh -T [email protected]

to see whether you're authenticated with gitlab.com correctly. Otherwise, make sure you've added your publickey to your GitLab account.

Use git+ssh://

Use git+ssh://, instead of git://, for example:

npm i -S git+ssh://[email protected]/my-project/repo.git

See: Install npm module from GitLab private repository.

Provide different identity via SSH

To list your current identities, run:

ssh-add
ssh-add -L

To add different identity, run: ssh-add ~/.ssh/MyOtherKey.pem

Check npm

Also, make sure that you are not using any proxy for your npm. Check the config by npm config list.

Use npm Enterprise

You can use npm Enterprise to connect to your existing authentication system, such as OAuth2 (for GitLab), GitHub Enterprise and other. You can also check for existing auth-plugin (for GitLab, see: npme-auth-gitlab), or write one for custom authentication.

Use personal token

As for workaround, you can create your personal or OAuth2 access token (GitLab/OAuth2; GitHub/OAuth) and use it along with your private repository URL. OAuth token will allow you to access repo via API, and personal token directly via URL, e.g.

  • GitHub: https://[email protected]/my-project/repo
  • GitLab: https://gitlab.com/my-project/repo?private_token=<PERSONAL_ACCESS_TOKEN>