How to get the information about an npm package
中文 Chinese Version
We can certainly look up information about a package by visiting the npm official website (opens in a new tab), but this approach has some drawbacks.
- You need to open a tab in the browser to look up the package
- You need to click on the "Dependency" navigation bar to view the Dependency
- Dependency only shows package name, no other information
- No Github star number
- ...
It is easier to check the information of a package by aggregating Dependency and github information in an API-like way,
Npm registry api
Npm official api (opens in a new tab) support CORS (opens in a new tab)
For example, Fetching next
package information with the URL https://registry.npmjs.org/next/latest (opens in a new tab), and this is the simple result
{
"name": "next",
"version": "12.2.5",
"description": "The React Framework",
"repository": { "type": "git", "url": "git+https://github.com/vercel/next.js.git" },
"homepage": "https://nextjs.org",
"dependencies": { "@next/env": "12.2.5", "@swc/helpers": "0.4.3" }
}
We can get the version, repository address, dependencies and other information from this simple result.
Github api
You can fetch github information through its official api.
Github api doesn’t support CORS (opens in a new tab), and there is a rate limit. Restful only has 60 requests per hour without the github authorization. Graphql must be logged in to use.
Fetch Github Document
Getting information by fetching Github document like a web crawler.
This method does not support CORS (opens in a new tab).
For example:
First, get the git repository document for nextjs by the url https://github.com/vercel/next.js.
Second, parse out document result via DOMParser (opens in a new tab)(in browse) or jsdom (opens in a new tab)(in nodejs), and then get data from dom element . For example, the text content of the #repo-stars-counter-star
element is the stars count of the repository.
PDJ
PDJ is a tool that aggregates information about an npm package, it extracts the information and displays only the basic information about a package:.
- Introduction
- npm weekly downloads
- github star count
- Denpendency list, including the description of each dependent package
PDJ has a Web App (opens in a new tab) and also a Chrome extension (opens in a new tab). The Chrome extension supports checking the package.json on a github page to display the above mentioned information.