Installing private GitHub repos as dependencies via NPM

I’m stuck trying to set up my project with private GitHub repos as dependencies. I’ve been at it for hours but no luck. Here’s what I’ve tried:

npm install git+https://github.com/myaccount/myproject.git

And my package.json looks like this:

{
  "dependencies": {
    "first-repo": "git+https://github.com/myaccount/first-repo.git",
    "second-repo": "git+https://github.com/myaccount/second-repo.git"
  }
}

Can anyone point me in the right direction? What am I missing here? I feel like I’m close but just can’t get it to work. Any help would be awesome!

Have you tried using SSH instead of HTTPS for your GitHub URLs? This approach often works better for private repos. You’ll need to set up SSH keys first if you haven’t already. Then, modify your package.json to use SSH URLs like this:

{
  "dependencies": {
    "first-repo": "git+ssh://[email protected]/myaccount/first-repo.git",
    "second-repo": "git+ssh://[email protected]/myaccount/second-repo.git"
  }
}

Make sure your SSH key is added to your GitHub account and your SSH agent is running. Then try ‘npm install’ again. This method has worked reliably for me across multiple projects with private dependencies.

It can be really challenging when you run into issues with private GitHub repositories and npm. In my experience, the solution was to use a personal access token for authentication rather than relying on the default URL. I generated a token on GitHub with the proper permissions and then adjusted the dependency URL in my package.json to incorporate the token in the URL using the format git+https://${GITHUB_TOKEN}:[email protected]/myaccount/my-private-repo.git. After setting the corresponding environment variable with my token, npm install was able to properly authenticate and access the repository. Always make sure to safeguard your token and never include it in version control.

hey mate, have u tried using a personal access token? i had similar issues n fixed em by using a token. generate one on github, then use it in ur package.json like this:

git+https://${TOKEN}:[email protected]/myaccount/repo.git

remember to set the TOKEN as an environment variable. hope this helps!