npm install of a git dependency stops with code 128
Permission 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.
Fix it fast
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.git2Match what you see, then jump to its fix
- 1
Permission denied (publickey).The URL uses SSH and git has no key GitHub acceptsFix - 2
Permission denied (publickey).The URL contains your GitHub username instead of gitFix - 3
Permission denied (publickey). only when the command starts with sudoThe install runs under sudo, so your user's key is not usedFix - 4
git asks for a username and password, or fails with no prompt in CIA private repository over HTTPS with no tokenFix
- 1
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 sha2562Fix
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.com3Verify
The test greets you by username, then install again.
ssh -T git@github.com
npm installConfirmed by docs.github.com
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.json2Fix
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 installConfirmed by docs.npmjs.com
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 install3Verify
Run the clone test as the same user that runs npm.
git ls-remote git@github.com:OWNER/REPO.gitConfirmed by docs.github.com
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.git2Fix
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.git3Verify
The ls-remote command lists refs without a prompt, then reinstall.
npm installConfirmed by docs.github.com
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
Back end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fixBack end
fluent-ffmpeg cannot find the ffmpeg or ffprobe binary
Error: Cannot find ffprobe
fluent-ffmpeg does not bundle ffmpeg.
4 causes, each with a fix