What is update-check?
The update-check npm package is used to check for updates of a particular package. It is typically used in command-line applications to inform users when a new version of the application is available. The package provides a simple API to check a registry (like npm) for published updates.
What are update-check's main functionalities?
Check for updates
This feature allows you to check if there is an update available for a given package. You provide the current package name and version, and it returns an object with the latest version if an update is available.
const updateCheck = require('update-check');
(async () => {
let update = await updateCheck({name: 'your-package-name', version: 'current-version'});
if (update) {
console.log(`Update available: ${update.latest}`);
}
})();
Custom registry
This feature allows you to specify a custom registry URL to check for updates, which can be useful if you are using a private registry or a registry other than npm.
const updateCheck = require('update-check');
(async () => {
let update = await updateCheck({name: 'your-package-name', version: 'current-version'}, {registryUrl: 'https://custom-registry.com'});
if (update) {
console.log(`Update available from custom registry: ${update.latest}`);
}
})();
Dist-tag
This feature allows you to check for updates under a specific distribution tag. This is useful when you want to check for updates that are not necessarily the 'latest' according to semver, but are tagged differently, like 'beta' or 'next'.
const updateCheck = require('update-check');
(async () => {
let update = await updateCheck({name: 'your-package-name', version: 'current-version', distTag: 'next'});
if (update) {
console.log(`Update available on dist-tag 'next': ${update.latest}`);
}
})();
Other packages similar to update-check
check-update
Similar to update-check, check-update allows you to check for package updates. It provides a simple API and can be used in a similar way to update-check. The main difference may lie in the API design and additional options provided.
npm-check
npm-check is a utility that checks for outdated, incorrect, and unused dependencies. It is more comprehensive than update-check as it not only checks for updates but also analyzes the status of dependencies used in a project.
npm-check-updates
npm-check-updates upgrades your package.json dependencies to the latest versions, ignoring specified versions. It goes beyond the functionality of update-check by not only checking for updates but also upgrading the dependencies in your package.json file.
update-check
This is a very minimal approach to update checking for globally installed packages.
Because it's so simple, the error surface is very tiny and your user's are guaranteed to receive the update message if there's a new version.
You can read more about the reasoning behind this project here.
Usage
Firstly, install the package with yarn...
yarn add update-check
...or npm:
npm install update-check
Next, initialize it.
If there's a new update available, the package will return the content of latest version's package.json
file:
const pkg = require('./package');
const checkForUpdate = require('update-check');
let update = null;
try {
update = await checkForUpdate(pkg);
} catch (err) {
console.error(`Failed to check for updates: ${err}`);
}
if (update) {
console.log(`The latest version is ${update.latest}. Please update!`);
}
That's it! You're done.
Configuration
If you want, you can also pass options to customize the package's behavior:
const pkg = require('./package');
const checkForUpdate = require('update-check');
let update = null;
try {
update = await checkForUpdate(pkg, {
interval: 3600000,
distTag: 'canary'
});
} catch (err) {
console.error(`Failed to check for updates: ${err}`);
}
if (update) {
console.log(`The latest version is ${update.latest}. Please update!`);
}
Contributing
- Fork this repository to your own GitHub account and then clone it to your local device
- Link the package to the global module directory:
npm link
- Within the module you want to test your local development instance of the package, just link it:
npm link update-check
. Instead of the default one from npm, node will now use your clone.
Author
Leo Lamprecht (@notquiteleo) - ZEIT