I’m trying to figure out how to add a private GitHub repository as a dependency in my package.json file. I’ve been looking at different ways to reference it but keep running into issues. When I try to use the standard GitHub URL format like username/repository-name, npm install fails with errors saying it can’t access the private repo. I’ve also tried different variations of the syntax but nothing seems to work. Is there a specific way to reference private repositories in the dependencies section? Do I need to configure authentication somehow or use a different format? Any help would be appreciated since I need to include several private repos in my project.
Had the same issue last week! You can also use the github: prefix like "github:username/repo-name" in your package.json. Works great if you’ve already got SSH keys set up. Just make sure your SSH agent’s running or it’ll fail during install.
Another thing that’s worked well for me - use the git URL with a specific branch or tag. Sometimes the default branch screws up authentication even when your credentials are fine. Try git+https://github.com/username/repository.git#branch-name or git+https://github.com/username/repository.git#v1.0.0 for tagged versions. Really helpful when you’re juggling multiple private repos with different default branches. One heads up though - if you’re using npm workspaces or deploying to CI/CD, make sure your auth method works everywhere. SSH keys are way trickier to manage in automated deployments than tokens.
Your issue is probably repository access permissions. For private GitHub repos in package.json, you need proper authentication. With HTTPS, use git+https://[token]@github.com/username/repository.git - just swap [token] for a personal access token with the right permissions. SSH keys work better though: git+ssh://[email protected]/username/repository.git. Make sure your token has repo scope or you’ll run into problems.