Use Github Package Registry and npmjs together

Emanuele Pecorari
3 min readNov 12, 2019

--

In the previous article https://medium.com/@emanuele.pecorari/move-from-npmjs-to-github-package-registry-in-5-steps-ac09ad17f25c we have seen how to use the Github package registry from our Node.js application.

I’ve received some questions asking if it would be still possible to use other package registries. I didn’t know the answer so I tried to find a solution extending my previous example to use a package from Github and another from npmjs.

Step1: use the scope

It’s possible to configure .npmrc to use different registries if we leverage the packages’ scope.

My previous package had the scope @manupeco so I modified my .npmrc as following:

//registry.npmjs.org/:_authToken=<NPM_AUTH_TOKEN>
@manupeco:registry=https://npm.pkg.github.com/manupeco
//npm.pkg.github.com/:_authToken=<PERSONAL_ACCESS_TOKEN>

Now, for the Github will be packages source for the scope @manupeco while npmjs will be used for the others

Step2: create a package for npmjs

To test it, I’ve created a new node package with the following index.js:

function NamePrinter() {
this.writeName = (name) => {
console.log(`Your name is ${name}. Package from npmjs`)
}
}
module.exports = NamePrinter

and a package.json file like this:

{
"name": "nameprinter-npmjs",
"version": "1.0.1",
"description": "Our npmjs package for nameprinter",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

and then I’ve published it on npmjs using

npm publish

Step3: use both packages in the app

I’ve modified the application (previously using only the package from the Github Registry) to use both the packages.

This is the application package.json:

{
"name": "githubpackagetest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@manupeco/nameprinter": "^1.0.6", // package from Github registry
"nameprinter-npmjs": "^1.0.1" // package from npmjs
}
}

After this I’ve run

npm install

obtaining the following node_modules folder:

Then I’ve changed the code:

const NamePrinter = require('@manupeco/nameprinter')
const NamePrinterJs = require('nameprinter-npmjs')
const printer = new NamePrinter()
printer.writeName("Emanuele")
const printerJS = new NamePrinterJs()
printerJS.writeName("Emanuele")

Running the app with

node index.js

the result is the following:

so the 2 packages are correctly installed and executed.

Conclusion

The usage of the Github Package Registry doesn’t exclude the other registries like npmjs if you use the scope to separate the connections.

Photo by Artem Sapegin on Unsplash

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Emanuele Pecorari
Emanuele Pecorari

Written by Emanuele Pecorari

Cloud Architect and Tech Product Owner. Soccer player and coach in the free time.

No responses yet

Write a response