Installing npm packages with GitHub dependencies on Heroku

I’m having trouble deploying my Node.js app to Heroku. My package.json includes a GitHub dependency:

{
  "dependencies": {
    "express": "4.17.1",
    "lodash": "4.17.21",
    "my-custom-lib": "github:myusername/my-custom-lib#main"
  }
}

It works fine on my local machine but fails on Heroku due to a git checkout error. The logs show:

npm ERR! git checkout main fatal: Not a git repository: '.'
npm ERR! Failed to install dependencies

I’ve updated npm on Heroku, but the issue persists and I can’t access the detailed log file in /tmp. Has anyone managed to deploy a Node.js app with GitHub dependencies on Heroku? Any advice or workarounds would be appreciated.

I’ve encountered this issue before when deploying to Heroku. One effective workaround is to use a GitHub release instead of a specific branch. In your package.json, try replacing the dependency line with:

“my-custom-lib”: “myusername/my-custom-lib#v1.0.0”

Replace ‘v1.0.0’ with the appropriate release tag. This approach often resolves git checkout errors on Heroku, as it doesn’t require cloning the entire repository. It’s more stable and can improve deployment times. If you haven’t created a release for your custom lib yet, it’s a good practice to start doing so for better version control and easier deployments.

I’ve dealt with similar GitHub dependency issues on Heroku before. One approach that worked for me was using the SSH URL format in the package.json file. Try modifying your dependency like this:

“my-custom-lib”: “git+ssh://[email protected]:myusername/my-custom-lib.git#main”

This method sometimes bypasses the git checkout errors. Make sure your Heroku app has the necessary SSH keys set up to access your GitHub repository. You can do this by adding your Heroku app’s SSH key to your GitHub account or using a deploy key for the specific repo.

Also, double-check that the Heroku Node.js buildpack is up to date. You can force an update with:

heroku buildpacks:set heroku/nodejs

If these don’t work, consider temporarily forking the dependency into a public repo or hosting it on npm as a private package. These alternatives can be more stable for Heroku deployments.

hey, i’ve had similar issues b4. try using a tarball URL instead of the git URL in ur package.json. like this:

“my-custom-lib”: “https://github.com/myusername/my-custom-lib/tarball/main

it worked for me when i had git checkout probs on heroku. hope this helps!