├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 21 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import type {Options as PackageJsonOptions} from 'package-json'; 2 | 3 | export {PackageNotFoundError, VersionNotFoundError} from 'package-json'; 4 | 5 | export type Options = Pick; 6 | 7 | /** 8 | Get the latest version of an npm package. 9 | 10 | @example 11 | ``` 12 | import latestVersion from 'latest-version'; 13 | 14 | console.log(await latestVersion('ava')); 15 | //=> '6.1.1' 16 | 17 | console.log(await latestVersion('@sindresorhus/df')); 18 | //=> '4.0.0' 19 | 20 | // Also works with semver ranges and dist-tags 21 | console.log(await latestVersion('npm', {version: 'latest-5'})); 22 | //=> '5.10.0' 23 | ``` 24 | */ 25 | export default function latestVersion(packageName: string, options?: Options): Promise; 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import packageJson from 'package-json'; 2 | 3 | export {PackageNotFoundError, VersionNotFoundError} from 'package-json'; 4 | 5 | export default async function latestVersion(packageName, options) { 6 | const {version} = await packageJson(packageName.toLowerCase(), options); 7 | return version; 8 | } 9 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import latestVersion from './index.js'; 3 | 4 | expectType>(latestVersion('ava')); 5 | expectType>(latestVersion('npm', {version: 'latest-5'})); 6 | expectType>(latestVersion('npm', {registryUrl: 'https://registry.yarnpkg.com'})); 7 | expectType>(latestVersion('npm', {omitDeprecated: false})); 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "latest-version", 3 | "version": "9.0.0", 4 | "description": "Get the latest version of an npm package", 5 | "license": "MIT", 6 | "repository": "sindresorhus/latest-version", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "latest", 31 | "version", 32 | "npm", 33 | "pkg", 34 | "package", 35 | "package.json", 36 | "current", 37 | "module" 38 | ], 39 | "dependencies": { 40 | "package-json": "^10.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^6.1.1", 44 | "semver": "^7.6.0", 45 | "semver-regex": "^4.0.5", 46 | "tsd": "^0.30.7", 47 | "xo": "^0.57.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # latest-version 2 | 3 | > Get the latest version of an npm package 4 | 5 | Fetches the version directly from the registry instead of depending on the massive [npm](https://github.com/npm/npm/blob/8b5e7b6ae5b4cd2d7d62eaf93b1428638b387072/package.json#L37-L85) module like the [latest](https://github.com/bahamas10/node-latest) module does. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install latest-version 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import latestVersion from 'latest-version'; 17 | 18 | console.log(await latestVersion('ava')); 19 | //=> '6.1.1' 20 | 21 | console.log(await latestVersion('@sindresorhus/df')); 22 | //=> '4.0.0' 23 | 24 | // Also works with semver ranges and dist-tags 25 | console.log(await latestVersion('npm', {version: 'latest-5'})); 26 | //=> '5.10.0' 27 | ``` 28 | 29 | This package exposes the [`version`](https://github.com/sindresorhus/package-json#version), [`registryUrl`](https://github.com/sindresorhus/package-json#registryurl), and [`omitDeprecated`](https://github.com/sindresorhus/package-json#omitdeprecated) options from [`package-json`](https://github.com/sindresorhus/package-json#options), as well as the [`PackageNotFoundError`](https://github.com/sindresorhus/package-json#packagenotfounderror) and [`VersionNotFoundError`](https://github.com/sindresorhus/package-json#versionnotfounderror) errors. 30 | 31 | ## Related 32 | 33 | - [latest-version-cli](https://github.com/sindresorhus/latest-version-cli) - CLI for this module 34 | - [package-json](https://github.com/sindresorhus/package-json) - Get the package.json of a package from the npm registry 35 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import semver from 'semver'; 3 | import semverRegex from 'semver-regex'; 4 | import latestVersion, {PackageNotFoundError, VersionNotFoundError} from './index.js'; 5 | 6 | test('latest version', async t => { 7 | t.regex(await latestVersion('ava'), semverRegex()); 8 | }); 9 | 10 | test('latest version with version', async t => { 11 | t.true(semver.satisfies(await latestVersion('package-json', {version: '0'}), '0.x')); 12 | }); 13 | 14 | test('latest version with dist-tag', async t => { 15 | t.true(semver.satisfies(await latestVersion('npm', {version: 'latest-5'}), '5.x')); 16 | }); 17 | 18 | test('latest version scoped', async t => { 19 | t.regex(await latestVersion('@sindresorhus/df'), semverRegex()); 20 | }); 21 | 22 | test('registry url', async t => { 23 | t.regex(await latestVersion('npm', {registryUrl: 'https://registry.yarnpkg.com/'}), semverRegex()); 24 | }); 25 | 26 | test('include deprecated', async t => { 27 | t.regex(await latestVersion('querystring', {version: '0.2', omitDeprecated: false}), semverRegex()); 28 | }); 29 | 30 | test('throws if not found', async t => { 31 | await t.throwsAsync(latestVersion('nnnope'), {instanceOf: PackageNotFoundError}); 32 | await t.throwsAsync(latestVersion('npm', {version: '0.0.0'}), {instanceOf: VersionNotFoundError}); 33 | }); 34 | --------------------------------------------------------------------------------