npmrc

Sometimes, when I run yarn for a project that uses a private dependency (whose registry access requires an auth token), I run into the 401 error:

yarn install v1.22.19
[1/4] πŸ”  Resolving packages...
[2/4] 🚚  Fetching packages...
error An unexpected error occurred: "https://npm.pkg.github.com/download/@myorg/mypackage: Request failed \"401 Unauthorized\"".

Usually this doesn’t happen if we have a proper .npmrc file somewhere up the root of the workdir (typically in the user home directory). But sometimes I am forced to work in a new setup (e.g. a repo cloned into a network drive, or an external drive), and if I manage to forget the .npmrc file, then it means some undesirable head scratching time.

The solution? Easy, just ensure the .npmrc file exists up the workdir chain. In my case, it works when I place it in a parent directory of my workdir, but I strongly believe it can also be in the workdir itself (i.e. the top-level project directory) provided we take care to .gitignore it.

As for the content of the .npmrc file, consider this:

//npm.pkg.github.com/:_authToken=<myRegistryAccessToken>
@myorg:registry=https://npm.pkg.github.com/

with myRegistryAccessToken being the GitHub access token that allows downloading (reading) from its npm package registry.

NB: also don’t forget to replace @myorg with the actual Org name πŸ˜‰

And that’s it. Just a quick post for my future self, lest I’m getting more senile…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s