DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

Collapse
 
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:

   //npm.pkg.github.com:_authToken=${NPM_TOKEN}
   repo:registry=//npm.pkg.github.com
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mauriciord profile image
Mauricio Reatto Duarte

this script is wrong

fs.writeFileSync(fileLocation, contents);

where is the fileLocation declaration?

Collapse
 
johan_du_toit profile image
Johan du Toit Author • Edited on

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

Collapse
 
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...

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.