Update notifications for your CLI app
npx @tessl/cli install tessl/npm-update-notifier@7.3.0Update Notifier provides non-intrusive update notifications for CLI applications. It asynchronously checks npm registries for newer versions in background processes without impacting application startup performance, then displays notifications to users when updates are available.
npm install update-notifierimport updateNotifier from "update-notifier";For CommonJS:
const updateNotifier = require("update-notifier");import updateNotifier from "update-notifier";
import packageJson from "./package.json" assert { type: "json" };
// Basic usage - checks for updates and displays notification
updateNotifier({ pkg: packageJson }).notify();
// Advanced usage - access update information
const notifier = updateNotifier({ pkg: packageJson });
if (notifier.update) {
console.log(`Update available: ${notifier.update.latest}`);
}Creates an UpdateNotifier instance and initiates background update check.
/**
* Creates UpdateNotifier instance and calls check() automatically
* @param options - Configuration options
* @returns UpdateNotifier instance
*/
function updateNotifier(options: UpdateNotifierOptions): UpdateNotifier;
interface UpdateNotifierOptions {
/** Package information */
pkg: {
/** Package name (required) */
name: string;
/** Current package version (required) */
version: string;
};
/** Update check interval in milliseconds (default: 86400000 - 1 day) */
updateCheckInterval?: number;
/** Allow notifications when running as npm script (default: false) */
shouldNotifyInNpmScript?: boolean;
/** NPM dist-tag to check for updates (default: "latest") */
distTag?: string;
/** @deprecated Use pkg.name instead */
packageName?: string;
/** @deprecated Use pkg.version instead */
packageVersion?: string;
}Main class for managing update notifications with configurable behavior and persistent storage.
class UpdateNotifier {
/** Configuration storage instance (undefined if disabled) */
config?: ConfigStore;
/** Update information when available */
update?: UpdateInfo;
/** Package name (semi-private, used in tests) */
_packageName: string;
/** Whether to notify in npm scripts (semi-private, used in tests) */
_shouldNotifyInNpmScript: boolean;
/**
* @param options - Configuration options
* @throws {Error} When pkg.name or pkg.version is missing
*/
constructor(options: UpdateNotifierOptions);
}Initiates background check for package updates using detached child processes.
/**
* Spawns background process to check for updates
* Respects updateCheckInterval and user preferences
*/
check(): void;Directly retrieves latest version information from npm registry.
/**
* Directly fetch update information from npm registry
* @returns Promise resolving to update information
*/
fetchInfo(): Promise<UpdateInfo>;
interface UpdateInfo {
/** Latest version available */
latest: string;
/** Current installed version */
current: string;
/** Update type: "latest" | "major" | "minor" | "patch" | "prerelease" | "build" */
type: string;
/** Package name */
name: string;
}Shows formatted update notifications with customizable messages and styling.
/**
* Display update notification to user
* @param options - Notification options
* @returns this (for method chaining)
*/
notify(options?: NotifyOptions): UpdateNotifier;
interface NotifyOptions {
/** Defer notification until process exit (default: true) */
defer?: boolean;
/** Custom notification message template */
message?: string;
/** Whether package is globally installed (auto-detected) */
isGlobal?: boolean;
/** Boxen styling options */
boxenOptions?: {
padding?: number;
margin?: number;
textAlignment?: string;
borderColor?: string;
borderStyle?: string;
};
}Template placeholders available for custom notification messages.
// Available template placeholders:
// {packageName} - Package name
// {currentVersion} - Current version
// {latestVersion} - Latest version
// {updateCommand} - Recommended update command
// Example custom message:
notifier.notify({
message: "Run `{updateCommand}` to update {packageName} from {currentVersion} to {latestVersion}."
});Users can opt out via configuration file or environment variables:
~/.config/configstore/update-notifier-{package-name}.jsonNO_UPDATE_NOTIFIER (any value disables)--no-update-notifierUpdate notifications are automatically disabled in:
is-in-ci)NODE_ENV=test)shouldNotifyInNpmScript: true)// Configuration stored in ~/.config/configstore/update-notifier-{package-name}.json
interface ConfigStoreData {
/** User opt-out preference */
optOut: boolean;
/** Timestamp of last update check */
lastUpdateCheck: number;
/** Cached update information */
update?: UpdateInfo;
}Basic CLI integration:
import updateNotifier from "update-notifier";
import packageJson from "./package.json" assert { type: "json" };
// Check and notify on app startup
updateNotifier({ pkg: packageJson }).notify();Custom notification message:
const notifier = updateNotifier({ pkg: packageJson });
notifier.notify({
message: "🚀 {packageName} v{latestVersion} is available! Run `{updateCommand}` to update.",
boxenOptions: {
borderColor: "green",
borderStyle: "double"
}
});Weekly update checks:
const notifier = updateNotifier({
pkg: packageJson,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
});Programmatic update checking:
const notifier = updateNotifier({ pkg: packageJson });
// Check for updates programmatically
const updateInfo = await notifier.fetchInfo();
if (updateInfo.type !== "latest") {
console.log(`Update available: ${updateInfo.current} → ${updateInfo.latest} (${updateInfo.type})`);
}Testing with immediate checks:
import updateNotifier from "update-notifier";
// For testing purposes - bypasses normal interval
const notifier = updateNotifier({
pkg: { name: "your-package", version: "1.0.0" },
updateCheckInterval: 0 // Check immediately
});
// Note: Updates won't show on first run (by design)
// Run your app twice to see notificationsLegacy usage (deprecated options):
// Still supported for backwards compatibility
updateNotifier({
packageName: "your-package", // @deprecated - use pkg.name
packageVersion: "1.0.0", // @deprecated - use pkg.version
updateCheckInterval: 86400000
}).notify();