Here is another solution, but there is a catch.
Change your registry.npmjs.org
package content to
index.js
export * from '@schotsl/my-package';
Now your registry.npmjs.org
package is (almost) pointing to your npm.pkg.github.com
package.
Only almost because any development directory for a project downstream of registry.npmjs.org/my-package
, must configure the scope-to-server mapping for @schotsl/my-package
to npm.pkg.github.com
in a package manager config file.
In the case of package managers 'npm' and 'yarn' (v1) that can be done in
an .npmrc
file at the same level as package.json
.
The required .npmrc
content is
@schotsl:registry=https://npm.pkg.github.com
# Github PAT token, packages:read authorization only ok
//npm.pkg.github.co/:_authToken="ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
The first line is the scope to server mapping.
The second line is a Github personal authorization token (PAT) with at least package:read
permission. It is actually pretty liberal. A PAT with package:read issued from any github account will allow read access to every github accounts packages.
For the 'yarn' v2 package, the .npmrc
file does not work, and instead a couple of keys need to be set in .yarnrc.yml
.
Unfortunately there is no way to set the scope-to-server mapping and the token inside the registry.npmjs.org/my-package
package itself.
Putting the .npmrc
file in there doesn't work, it is ignored. And that wouldn't be a good solution anyway, because not all package managers read .npmrc
files.
That is the 'catch' - using npm.pkg.github.com packages requires package manager specific config settings to be made by every downstream developer.
In addition, what if two different upstream packages have colliding scope names, each mapping to a different server? The current config methodology fails in that case.
Feature Proposal not current behavior
Ideally, there would be a common interface agreed upon by all package managers inside package.json
- and the scope-to-server mapping would be defined in the package that directly references the scope. For example, in the package.json
of my-package
on registry.npmjs.org
{
dependencies:{
"@schotsl/my-package":"1.0.0"
},
registries:{
"@schotsl/my-package":"https://npm.pkg.github.com",
},
auths:{
"https://npm.pkg.github.com":"ghp_XXXXXXXXXXXXXXXX",
},
}
Then downstream users would not need to config for each scope, and predictable (and risky) problems with scope name or package name collisions would not occur.
But that is not the way it is. Therefore Github Packages (npm.pkg.github.com) doesn't really seem to be a feasible way to provide public packages which may become dependencies of other public packages. No problem for private packages though.