Accessing private Github package registry

Our site build fetches dependencies from a private Github package registry.
How do I set up the build to use Github access token to authenticate to GH packages?

Hi @mrslump and sorry to be slow to get back to you! None of our Support team use that GH feature, but we found someone who did and they clued us in to their workflow.

This is the flow that we use for one of our private GH0hosted dependencies. This is the entire npmrc on that codebase:

@netlify:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NPM_TOKEN} 

We set $NPM_TOKEN in our site environment variables, on the build & deploy settings page, and then .npmrc lives next to package.json and things should Just Work.

Note that if you use that locally, you will need to have $NPM_TOKEN set locally too! Should work the same on Netlify and locally :slight_smile:

1 Like

Thanks!

In dev sandboxes we configure the NPM token in ~/.npmrc and would like to have it working in local environments without having to tweak additional environment variables.
I was planning to create a script for setting this up when building on Netlify.

However, I’m currently having some problems with this.
It seems that right after Netlify clones my git repo it runs yarn install automatically and only then runs the build script I’ve configured.

Is it possible to get a custom command/script to run before or instead of yarn install?

Unfortunately no, you cannot do anything before we install dependencies automatically UNLESS your site has no dependency installation config (no package.json/yarn.lock), which seems like it isn’t an option for you.

We are working to extend build plugins (Build Plugins | Netlify Docs) to allow scripts to run before dependency installation, and I will report back in this thread once we ship that feature, since it will unblock you in some ways at least.

I don’t know a good way to avoid using npmrc, so not sure how to unblock you further on that requirement around your local env.

Thanks for the update!

Can you give any tentative estimates on what the schedule for making this feature available could be?

hi @mrslump, you might keep an eye here:

1 Like

Just to make sure your question got an answer: we don’t promise delivery dates for code not yet written yet. Following that issue will be your best indicator of work progressing :slight_smile:

1 Like

I’ve tried this, and it seems to install the dependency correctly after adding a Personal Access Token as the NPM_TOKEN variable in the Build>Environment settings, but then, on the build command, it fails with Cannot find module '...' or its corresponding type declarations.. :frowning:

For support team only:

Hiya @paulmelero and sorry to be slow to get back to you! I think that if the request succeeds (rather than showing an auth error), you’ll probably want to debug what is installed, where as next steps. It’s my guess that it is somehow ending up in the wrong directory, which is not something we control (your build pipeline does).

Have you tried running your build locally in our build container to see where the output goes, and if it is in a place your build pipeline can find it? If you don’t have local docker, you could get some debug details out of our CI instead with a build command more like:

npm run build ; ls -lR && cat /opt/buildhome/.npm/_logs/*debug.log && sleep 200 ; false

This will start your build (we never get to step 2 npm run export as far as I can tell), and then show you what files are there, what the debug logs from npm show, and give all that output time to get saved, before intentionally failing the build (since we wouldn’t want to publish your build if the ‘sleep’ command succeeds - it always will).

It looks like this guide should be the definitive answer: Using GitHub Package Registry with Netlify

1 Like