28

I created and published a private Github package. Trying to install it with yarn at first, I face the following issue:

Whether I try with yarn or npm, it cannot find the package at all although following the exact steps documented by Github (https://help.github.com/en/github/managing-packages-with-github-package-registry/configuring-npm-for-use-with-github-package-registry).

My .yarnrc:

registry "https://npm.pkg.github.com/OWNER"

With yarn, it continuously tries to look for the package at https://registry.yarnpkg.com/@GITHUB_USERNAME instead of the registry I entered above.

Remark: in .yarnrc registries need to be added following a slightly different syntax:

registry "https://npm.pkg.github.com/"

So far I started to also play around with a mix of .npmrc and .yarnrc configurations but no luck.

-

EDIT (Partly solved)

I figured out how to actually access the package, both using npm or - in my case - yarn. Now I face the issue of a Request failed \"401 Unauthorized\" error, although I added the credentials on top of .yarnrc:

//npm.pkg.github.com/:_authToken=AUTH_TOKEN

Doing the same in .npmrc doesn't work either.

leonheess
  • 16,068
  • 14
  • 77
  • 112
alexeis
  • 2,152
  • 4
  • 23
  • 30
  • 1
    Vote on [this proposal](https://meta.stackoverflow.com/questions/354583/disentangle-the-yarn) to ease the tag confusion. – leonheess Feb 10 '20 at 12:37

4 Answers4

43

I found the solution which unfortunately is not well documented anywhere but a mix of different resources - and it's quite simple.

No matter whether you use npm or yarn, just have the following .npmrc in place (yarn will also include this):

registry=https://registry.yarnpkg.com/

@GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=AUTH_TOKEN
always-auth=true

Some comments:

  • always-auth is needed, at least when using yarn (haven't tested using npm)
  • Adding the above in the .yarnrc instead doesn't work. Somehow yarn has issues when authentication is needed.
  • Now you can easily install your private packages with yarn add @GITHUB_USERNAME/PACKAGE_NAME or the npm equivalent.
  • Include registry=https://registry.yarnpkg.com/ for yarn or registry=https://registry.npmjs.org/ for npm

I hope this solution works also in your case. Otherwise let me know what issues you face and I'm happy to share some of the research on this topic and where the solution may hide.

alexeis
  • 2,152
  • 4
  • 23
  • 30
  • Thanks, worked like a charm. Except I didn't need the `always-auth` part. Maybe something has changed in yarn since? – C S Aug 18 '20 at 23:04
8

I am adding an answer here because after a day of trying different variations of the solutions here and elsewhere, I found that my issue was something else.

My issue was that, while npm is not case sensitive with regards to package names, yarn is when it comes to authentication! ‍♂️

So, using the example from the accepted solution above:

registry=https://registry.yarnpkg.com/

@GITHUB_USERNAME:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=AUTH_TOKEN
always-auth=true

I needed to ensure two things:

  1. @GITHUB_USERNAME needs to match the case that you see on github and the name the package was published under. I.e., if your username is Pickle-Rick, you need to put @Pickle-Rick:registry=https://npm.pkg.github.com, not @pickle-rick or @Pickle-rick.

  2. You need to match this casing in your package.json or your yarn add command - whichever you are using. For example:

    "@Pickle-Rick/schwifty": "^1.0.0" in package.json or yarn add @Pickle-Rick/schwifty.

I found this solution by digging through yarn github issues.

elethan
  • 16,408
  • 8
  • 64
  • 87
  • This is the only answer that works for me, I tried others but they do nothing. Also I had to add this to my github actions .yaml file: `- name: Fix .npmrc for CI/CD run: | echo "//npm.pkg.github.com/:_authToken=${{ secrets.PACKAGE_TOKEN }}" >> ~/.npmrc` since it wouldn't otherwise authenticate properly – pizzae Mar 21 '21 at 08:04
  • 1
    This worked for me. Thank you so much for this answer. If you are using Yarn make sure the @GITHUB_USERNAME matches whats on github.com – Orbita1frame Sep 13 '22 at 20:09
2

What you need to do is specify where to retrieve EACH package, with something like this in your .npmrc (I don't know the yarn syntax, but it works with yarn when it reads the .npmrc):

//registry.npmjs.org/:_authToken=<token-npm-read>
//npm.pkg.github.com/:_authToken=<token-github-package-read>
@foo:registry=https://npm.pkg.github.com
@far:registry=https://registry.npmjs.org

And then, Yarn will search @foo/mypackage1 in Github, where @far/mypackage2 will be searched in npmjs. The default registry will be kept for the others, whatever you set it to.

Romain Prévost
  • 513
  • 2
  • 12
2

I faced the same issue during work with JFrog Artifactory...

When I used npm install or yarn install I always got errors:

 => ERROR [builder 4/6] RUN yarn install                                                                                                         61.0s
> [builder 4/6] RUN yarn install:
#12 0.358 yarn install v1.22.15
#12 0.474 [1/4] Resolving packages...
#12 1.199 [2/4] Fetching packages...
#12 6.330 error An unexpected error occurred: 
"https://customer.jfrog.io/dxg/api/npm/npm-local/@xx/components-react16/-/@xx/components-react16-1.2.0.tgz: 
Request failed \"401 Unauthorized\"".

I used generation of authToken and file .npmrc (user_home folder) with command:

npm login --registry=https://dxg.jfrog.net/dxg/api/npm/npm-local/ --scope=@xx

SOLUTION:

Exists approach when we could get all needed config for .npmrc (or yarnrc) file directly from the Artefactory:

curl -u<USER_NAME>:<AUTH_TOKEN> "https://<your_domain>/artifactory/api/npm/npm-local/auth/jfrog"

After getting data from the response you should put it to .npmrcfile manually.

for example approximated structure of response:

@xx:registry=https://<your_domain>/artifactory/api/npm/npm-local/
//<your_domain>/dxg/api/npm/npm-local/:_authToken=<auth_Token>
//<your_domain>/artifactory/api/npm/npm-local/:_password=<password>
//<your_domain>/artifactory/api/npm/npm-local/:username=<user_name>
//<your_domain>/artifactory/api/npm/npm-local/:email=<email>
//<your_domain>/artifactory/api/npm/npm-local/:always-auth=true
vvator
  • 168
  • 2
  • 11