Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

npm install of a git dependency stops with code 128

ErrorPermission denied (publickey).

Short answer

Code 128 is git failing, not npm. npm shells out to git to clone the dependency, and git cannot log in to that repository. Run the same clone yourself with the URL from package.json. The message you get tells you whether the SSH key is missing, a token is needed, or sudo hides your key.

  • Applies to npm 10, Node.js 22, git 2.4x, Ubuntu 24.04, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the git dependency, then run the same clone by hand. The error git prints is the real one; npm only passes on its exit code.

    grep -n "git" package.json
    git ls-remote git@github.com:OWNER/REPO.git
  2. 2Match what you see, then jump to its fix

    1. 1Permission denied (publickey).The URL uses SSH and git has no key GitHub acceptsFix
    2. 2Permission denied (publickey).The URL contains your GitHub username instead of gitFix
    3. 3Permission denied (publickey). only when the command starts with sudoThe install runs under sudo, so your user's key is not usedFix
    4. 4git asks for a username and password, or fails with no prompt in CIA private repository over HTTPS with no tokenFix

What does the error look like?

  • Permission denied (publickey).
  • npm install of a git dependency ends with code 128

Cause 1 of 4

The URL uses SSH and git has no key GitHub accepts

1Check

Ask GitHub who it thinks you are. A working key answers with a greeting that contains your username. Then check that a key is loaded into the SSH agent.

ssh -T git@github.com
eval "$(ssh-agent -s)"
ssh-add -l -E sha256

2Fix

Create an SSH key if none is listed, add the public key under Settings > SSH and GPG keys on GitHub, and load it with ssh-add. If the key has a custom file name, pass it with ssh -i to test it. In ssh -vT output, identity lines ending in -1 mean ssh found no key file.

ssh-add ~/.ssh/KEY-FILE
ssh -i ~/.ssh/KEY-FILE -vT git@github.com

3Verify

The test greets you by username, then install again.

ssh -T git@github.com
npm install

Confirmed by docs.github.com

Did this fix it?

Cause 2 of 4

The URL contains your GitHub username instead of git

1Check

An SSH URL must log in as the user git. GitHub rejects a login as your username, even with a valid key. Look at the dependency line.

grep -n "github.com" package.json package-lock.json

2Fix

Change the user part of the URL to git. npm's documented SSH form is git+ssh://git@github.com:OWNER/REPO.git, with an optional #commit-ish or #semver: suffix.

"dependencies": {
  "my-lib": "git+ssh://git@github.com:OWNER/REPO.git#v1.0.0"
}

3Verify

Reinstall and confirm the clone step passes.

npm install

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 4

The install runs under sudo, so your user's key is not used

1Check

A key generated without sudo is not used by a git command run with sudo. See whether you run npm with sudo or in a root shell.

whoami
history | grep "sudo npm"

2Fix

Run npm install as your normal user, without sudo. If you must use elevated rights, GitHub advises applying them to every command, so the same key is used throughout.

npm install

3Verify

Run the clone test as the same user that runs npm.

git ls-remote git@github.com:OWNER/REPO.git

Confirmed by docs.github.com

Did this fix it?

Cause 4 of 4

A private repository over HTTPS with no token

1Check

GitHub removed password login for Git. Over HTTPS, a private repository needs a personal access token at the password prompt, or a credential helper that supplies it. HTTPS also works behind firewalls and proxies.

git ls-remote https://github.com/OWNER/REPO.git

2Fix

Create a personal access token, enter it when git asks for the password, or let a credential helper such as Git Credential Manager store it. In CI, use an SSH deploy key or a token from the CI secret store instead of a prompt.

git ls-remote https://github.com/OWNER/REPO.git

3Verify

The ls-remote command lists refs without a prompt, then reinstall.

npm install

Confirmed by docs.github.com

Did this fix it?

How do I stop it coming back?

  • Pin git dependencies to a tag or commit with #commit-ish so installs are repeatable.
  • Use a published registry version when one exists, and keep git dependencies for forks you really need.
  • In CI, give the job a deploy key or token from secrets rather than a developer's personal key.
  • Keep the test command (git ls-remote with the same URL) in your onboarding notes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.