Skip to content

Instantly share code, notes, and snippets.

@GeoffreyBooth
Created July 7, 2022 19:00
Show Gist options
  • Save GeoffreyBooth/b453a465470eac38564f8379fa7e7dff to your computer and use it in GitHub Desktop.
Save GeoffreyBooth/b453a465470eac38564f8379fa7e7dff to your computer and use it in GitHub Desktop.
Downloads of npm packages by major version (last 7 days)
// Run this script via: node --no-warnings --experimental-fetch count-npm-package-downloads.mjs <package>
import { argv } from 'node:process'
const packageName = argv.at(-1)
const downloadsByMajorVersion = new Map()
const page = await (await fetch(`https://www.npmjs.com/package/${packageName}?activeTab=versions`)).text()
const regex = /">(?<version>[\d.]+)<\/a><div class="[^"]+"><\/div><code class="downloads">(?<downloads>[\d,]+)<\/code>/g
for (let match of page.matchAll(regex)) {
const majorVersion = match.groups.version.split('.')[0]
const downloads = Number(match.groups.downloads.replace(/,/g, ''))
downloadsByMajorVersion.set(majorVersion, (downloadsByMajorVersion.get(majorVersion) ?? 0) + downloads)
}
console.log(`Downloads per major version for package ${packageName}:`)
for (const [majorVersion, downloads] of downloadsByMajorVersion) {
console.log(`${majorVersion}.x: ${new Intl.NumberFormat().format(downloads)}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment