DEV Community

Cover image for Heroku & Github (private) packages - Just Work™
Johan du Toit
Johan du Toit

Posted on

Heroku & Github (private) packages - Just Work™

Cut to the chase

Like many developers out there, we work on side projects from time to time. Sometimes it's to learn, to have fun or to make money. Regardless of the reason, we often throw in the towel due to burnout, loss of interest or that frustrating service/tool that should just work™ but doesn't.

I recently murmured "Why isn't this just working as stated!" while attempting to get Heroku to install my private Github packages. I headed to my trusty ol' friend, Google, to scour the internet for an answer only to be repeatedly told that I need to 'commit an .npmrc file' with the registry URL (with auth token) and getting absolutely nowhere.

On the verge of giving up, I decided to head into the Github actions source code. After all I was able to publish my package to my private Github registry in a CI environment.

Below is the code that dynamically creates the .nmprc file before the Heroku build (using the heroku-prebuild hook), using values from the environment variables.

The good stuff

  1. Add the auth-npm.js script to your project (I add it to my bin directory)

  2. Update package.json

Drop a comment below if you're running into trouble and/or if you have improvements!

Remember to #KeepHavingFun

Top comments (4)

hutch78 profile image
Jeremy Hutchcraft

Thanks for the article, although I am struggling getting this to work -- any examples of what GITHUB_SCOPE might look like? I am outputting the contents variable after the fs.writeFileSync line and it looks like this:

Enter fullscreen mode Exit fullscreen mode
mauriciord profile image
Mauricio Reatto Duarte

this script is wrong

fs.writeFileSync(fileLocation, contents);

where is the fileLocation declaration?

johan_du_toit profile image
Johan du Toit • Edited

Probably way to late, but fileLocation should be the npmrc variable. Will update the script.

leesmith profile image
Lee Smith 🍻

Heroku clearly documents how to do this but it simply doesn't work for my review app. Quite the head scratcher...