3

Is there a way to determine a NPM package's source repository URL? Looking at the https://registry.npmjs.org API it doesn't point to the source for the package?

Am trying to figure out how to work back from a dependent package to the source for it - possibly in some automated way. Anyone have any insight they can share?

For example the npmjs.org page for react:

React's package at NPM

Clearly shows the "Repository" link. But the "registry" doesn't include this information. I want to get that Github url to https://github.com/facebook/react from the API/CouchDB database.

UPDATE: Have discovered that the "package" API does indeed provide data I am looking for.

curl https://replicate.npmjs.com/react | jq

Returns:

...
"repository": {
    "type": "git",
    "url": "git+https://github.com/facebook/react.git",
    "directory": "packages/react"
  }
...

But curl https://replicate.npmjs.com/_all_docs\?include_docs\=true -o npm.full.json doesn't include that info.

Kit Plummer
  • 583
  • 1
  • 3
  • 10
  • Do you want the URL of the repository, (such as a Github repo), for where the packages source code resides? Or the URL of the tarball (`.tgz` file) for the package which resides in the npm regsistry? Or something else? Perhaps you can clarify by providing an example of a package name and the value/URL that you expect? – RobC Apr 22 '20 at 07:03
  • Am specifically looking to get to the source code, so yeah Github, Bitbucket, Gitlab, or other repository. Updating the original post with more details. – Kit Plummer Apr 22 '20 at 15:31
  • @KitPlummer - So essentially, do you want to programmatically obtain the URL that running the `npm repo ` command, e.g. `npm repo react`, or `npm repo eslint` etc, opens your browser to? – RobC Apr 22 '20 at 16:39
  • @RobC - well that would work. :D Doesn't answer my question completely...I'm now even more baffled how the registry doesn't include this info. I have discovered a bit of new info though. – Kit Plummer Apr 22 '20 at 18:21

1 Answers1

1

The answer is that API that provides this info is the package directly:

curl https://replicate.npmjs.com/react | jq
Kit Plummer
  • 583
  • 1
  • 3
  • 10
  • 3
    Which is essentially the same as running: `npm view react repository` or `npm view react repository.url` - However, also as per your curl solution; the actually URL part needs to be inferred because the values vary if they exist. In [this code](https://github.com/npm/cli/blob/36682d4482cddee0acc55e8d75b3bee6e78fff37/lib/repo.js#L37) for the npm-cli tool they use the [hosted-git-info](https://github.com/npm/hosted-git-info) pkg , and its `browse()` method to try to convert the specified repository URL to an actual URL - if it can't be inferred they try again via the `unknownHostedUrl` function. – RobC Apr 23 '20 at 10:14