I recently found out that npm doesn’t support downloading same scope dependencies from multiple registries.

Ideally, I’d like to define multiple registries for a scope. If npm fails to find the package from registry1, then it should try registry2 and so on.

I use an @eisberg-labs scope for some private and some public packages.

Private packages are hosted on the github npm registry, and public on the npmjs.

Now, if I have an .npmrc saying

//npm.pkg.github.com/:_authToken=${GITHUB_READ_PACKAGES_TOKEN}
@eisberg-labs:registry=https://npm.pkg.github.com/

When downloading package with @eisberg-labs scope, npm will look no further than the configured registry, which is dissaponting.

Solution

Just publish a public package both to npmjs and github registry.

If anyone knows a better solution, please do share.

How to publish to a github npm registry

To be able to publish to a github private npm registry, add this to .npmrc:

//npm.pkg.github.com/:_authToken=${GITHUB_WRITE_PACKAGES_TOKEN}
@eisberg-labs:registry=https://npm.pkg.github.com/

Obtain a GITHUB_WRITE_PACKAGES_TOKEN by going to Settings -> Developer Settings -> Create new token and check write packages. Export the variable.

Note that .npmrc may be defined in your home or project directory.

As for installing from private npm registry, you’d define in project root an .npmrc with:

//npm.pkg.github.com/:_authToken=${GITHUB_READ_PACKAGES_TOKEN}
@eisberg-labs:registry=https://npm.pkg.github.com/

Publishing to a github npm registry is now easy as typing a npm publish.

Publish a public package to multiple registries

I first call npx release-it --dry-run and then without the --dry-run.

Then I go on to publish to the github registry.

npm config set _authToken=$GITHUB_WRITE_PACKAGES_TOKEN
npm login --registry=https://npm.pkg.github.com
npm publish --access public --@eisberg-labs:registry=https://npm.pkg.github.com

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *