Install GitHub Package: Request Failed \”401 Unauthorized\”
This is part of my error remedy reference collection. I create this series as a personal reference and I share it in the hope this series could help you fix bugs more easily.
The solution set forth in this article is intended for developers who are not using GitHub Actions. If you use GitHub actions see Authenticating with the GITHUB_TOKEN.
Description
When trying to install a private GitHub Package you need to point your package manager to GitHub Package Registry. In addition, you need to provide the right level of access to your package manager for the package manager to access private packages. On GitHub access can be provided by means of a personal access token.
If you don’t do this properly, you run into the following error:
An unexpected error occurred: “https://npm.pkg.github.com/download/@YOUR_ORGANISATION/PACKAGE_NAME/VERSION/HASH”: Request failed \”401 Unauthorized\””.

Solution
Step 1: Create a personal access token on GitHub with a read:packages scope
Step 1.1. Click here to go to the token page.
Step 1.2. Fill in a Note (i.e. name of the access token)
Step 1.3. Select scopes; If you don’t need to publish packages, you only need the read:packages scope.
Step 1.4. Click Generate token
Note: At the moment of writing GitHub only supports personal access tokens to grant read access to packages. Hopefully this changes in the future.
Step 2: Copy the generated token to your clipboard
Step 3: Create a .npmrc
Create a .npmrc by executing the following commands in your root folder.
In the code snippet below you should replace YOUR_TOKEN with the token copied to your clipboard and replace YOUR_ORGANISATION with your GitHub account name or the name of the organisation that holds the GitHub repository.
echo "//npm.pkg.github.com/:_authToken=YOUR_TOKEN" > .npmrc
echo "@YOUR_ORGANISATION:registry=https://npm.pkg.github.com/" >> .npmrc
System
NPM: 6.14.4
Yarn: 1.22.4
Circle CI
I encountered the error above in Circle CI. After solving this error my config.yml has a build step that looks as follows:
