Search code examples
node.jsgithubyarnpkgpackage.jsongithub-package-registry

Need to specify scope twice when adding library from GitHub Packages


In our GitHub org, we have a private repository for an internal Node.js library. I've set up an Actions Workflow to build and publish a package to the GPR whenever a release is cut and tagged. Following the docs here, I've scoped the package with our org name and specified the repository and publishConfig values. The package.json file for the library resembles this:

{
    "name": "@org-name/lib-name",
    "version": "1.1.1",
    "repository": "git://github.com/org-name/lib-name.git",
    "publishConfig": {
        "registry": "https://npm.pkg.github.com"
    },

    ...

}

I am encountering a problem when trying to add the library to another project using Yarn. Specifically, when using the yarn add command, I need to specify the scope twice in order for the library to be found, but this also causes further issues (presumably because the second '@' symbol is expected to indicate a version).

In the consuming project, I have added a .yarnrc file with the following entry for the GPR:

"@org-name:registry" "https:/npm.pkg.github.com"

If I then use the command yarn add @org-name/lib-name --verbose, I get the following:

[1/4] 🔍  Resolving packages...
verbose 0.645526824 Performing "GET" request to "https://npm.pkg.github.com/@org-name%2flib-name".
verbose 1.056449407 Request "https://npm.pkg.github.com/@org-name%2flib-name" finished with status code 401.
verbose 1.066291664 Error: Couldn't find package "@org-name/lib-name" on the "npm" registry.
    at MessageError.ExtendableBuiltin (/usr/local/Cellar/yarn/1.22.15/libexec/lib/cli.js:721:66)
    at new MessageError (/usr/local/Cellar/yarn/1.22.15/libexec/lib/cli.js:750:123)
    at NpmResolver.<anonymous> (/usr/local/Cellar/yarn/1.22.15/libexec/lib/cli.js:50864:15)
    at Generator.next (<anonymous>)
    at step (/usr/local/Cellar/yarn/1.22.15/libexec/lib/cli.js:310:30)
    at /usr/local/Cellar/yarn/1.22.15/libexec/lib/cli.js:321:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
error Couldn't find package "@org-name/lib-name" on the "npm" registry.

Whereas, if I use the command yarn add @org-name/@org-name/lib-name, it seems to find something but still doesn't work correctly:

yarn add v1.22.15
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @org-name/@1.0.1
info All dependencies
└─ @org-name/@1.0.1

Note that the actual library name is not referenced above. This is reflected the update to package.json dependencies which looks like this:

{
  ...
  "dependencies": {
    "@babel/core": "^7.16.0",
    "@org-name/": "org-name/lib-name",
    ...
  },
  ...
}

An @org-name folder has also been added under node_modules, the contents of which do resemble the lib-name project.

At this stage, I have read and experimented to the point that I'm getting lost trying the same things over and over again. I'm hoping someone can point me to the boneheadedly simple error I've made in my config above. Could anyone suggest something I could try from here? My expectation is to simply use yarn add @org-name/lib-name to consume the library in other projects.


Solution

  • You might be missing an auth token if that's a private package. Try adding this to your .npmrc:

    //npm.pkg.github.com/:_authToken=<<YOUR GITHUB PAT>>
    @org-name:registry=https://npm.pkg.github.com