问题
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.
回答1:
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 orregistry=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.
回答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.
来源:https://stackoverflow.com/questions/58522363/installing-private-package-from-github-package-registry-fails-with-not-found-not