Angular2+项目执行npm install遇Host key verification failed错误求助
npm install for Angular Projects Hey there! Let's break down this error you're seeing when running npm install in your Angular project—this is a Git SSH connectivity issue, not something specific to Angular itself. The error pops up because npm is trying to pull a private dependency from Bitbucket via SSH, but Git can't verify the Bitbucket server's host key, so it refuses to establish a secure connection.
Here's the error you shared for context:
npm ERR! Error while executing: npm ERR! C:\Program Files\Git\bin\git.EXE ls-remote -h -t ssh://git@bitbucket.org/path/projectname.git npm ERR! npm ERR! Host key verification failed. 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
Let's walk through the fixes step by step:
Step 1: Manually Trust Bitbucket's Host Key
The quickest fix is to let Git recognize Bitbucket's server key. Here's how:
- Open Git Bash (since your error uses Git's Windows executable)
- Run this command to test the SSH connection:
ssh git@bitbucket.org - When you see the prompt:
The authenticity of host 'bitbucket.org (IP_ADDRESS)' can't be established..., typeyesand press Enter. This adds Bitbucket's key to yourknown_hostsfile, so Git will trust it for future connections.
Step 2: Verify Your SSH Key is Linked to Bitbucket
If Step 1 doesn't resolve the issue, your SSH key might not be associated with your Bitbucket account:
- Generate an SSH key (if you don't already have one):
Just press Enter through all prompts to use default settings.ssh-keygen -t ed25519 -C "your-email@example.com" - Copy your public key:
- On Windows:
type C:\Users\YourUsername\.ssh\id_ed25519.pub - On macOS/Linux:
cat ~/.ssh/id_ed25519.pub
- On Windows:
- Add the key to Bitbucket:
Log into your Bitbucket account, go to Personal Settings > SSH keys, paste the copied public key, and save it.
Step 3: Confirm Repository Access & Path
Double-check these details to rule out simple mistakes:
- The repository path
bitbucket.org/path/projectname.gitis correct (typos in the path are surprisingly common!) - Your Bitbucket account has read permissions for the repository (if it's private, you need to be added as a member or have team access)
Step 4: Fallback: Switch to HTTPS Instead of SSH
If SSH keeps giving you trouble, you can modify the dependency URL in your package.json to use HTTPS:
- Replace the SSH URL
ssh://git@bitbucket.org/path/projectname.gitwith:git+https://bitbucket.org/path/projectname.git - When you run
npm installnext, you'll be prompted to enter your Bitbucket username and password (or an App Password if you have 2FA enabled on your account).
Once you've tried these steps, re-run npm install—the host key verification error should be gone. If you still hit snags, make sure your SSH agent is running (on Windows, you might need to start the OpenSSH Authentication Agent service manually).
内容的提问来源于stack exchange,提问作者hkaur13

