Querying information about specific version of scoped npm package

299 Views Asked by At

I can successfully query meta information for a specific version of a specific NPM package like this:

GET https://registry.npmjs.org/<name>/<version>

for example: https://registry.npmjs.org/camelcase/2.1.1

But for scoped packages like @angular/core this doesn't work. I tried all of the following, but they all fail:

What is the correct way for querying a specific version of a scoped package?

1

There are 1 best solutions below

3
On BEST ANSWER

You can do this from a bash command:

npm view @angular/core/6.1.10

So npm is adding some authentication to the request for scoped packages. For this to work you have to have a valid package.json in the local directory.

Of course, worst case, you can do a process.spawn() to run the npm command.

FYI, I tried using the npm-registry-client package, with my npm credentials:

var RegClient = require('npm-registry-client')
var client = new RegClient({
    username: 'mememe',
    password: 'xxxxx'
})
var uri = "https://registry.npmjs.org/@angular/core/6.1.10"
var params = {timeout: 1000}

client.get(uri, params, function (error, data, raw, res) {
  console.log(data);
})

and I got this:

info attempt registry request try #1 at 09:52:09
http request GET https://registry.npmjs.org/@angular/core/6.1.10
http 401 https://registry.npmjs.org/@angular/core/6.1.10
WARN notice ERROR: you cannot fetch versions for scoped packages

It appears they don't allow specific version querying, but per @RobC's comments below, they do allow grabbing the entire repository's information, so you can do this client-side:

url = 'https://registry.npmjs.org/@angular%2fcore';

const fetch = require('node-fetch');

fetch(url).then(response => response.json()).then(results => {
    console.log(results.versions['6.1.10']);
});