CLI Update Notifier (CommonJS)

5.1.7 · active · verified Wed Apr 22

update-notifier-cjs is a JavaScript library designed to provide non-intrusive update notifications for Command Line Interface (CLI) applications. Currently at version 5.1.7, it is specifically maintained for CommonJS (CJS) environments, providing a direct alternative to `update-notifier` for projects that do not use ES Modules. The library works by asynchronously checking for package updates against the npm registry in a detached child process, ensuring no impact on the CLI tool's startup performance. It persists the check results and displays notifications only after a configurable interval (defaulting to one day), preventing frequent interruptions to the user. This design choice aims to be user-friendly by avoiding immediate notifications on first run or every execution.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the update notifier and immediately check for and display update notifications for a CLI application using a simple `package.json` object. This example includes settings to facilitate testing.

const updateNotifier = require('update-notifier-cjs');
const pkg = {
  name: 'my-cli-app',
  version: process.env.TEST_APP_VERSION ?? '1.0.0',
  description: 'A simple CLI app'
};

// Simulate an older version to trigger an update notification for testing
// For a real app, use: const pkg = require('./package.json');

// Initialize the notifier with your package info
const notifier = updateNotifier({
  pkg,
  // Set a very short interval for testing purposes
  updateCheckInterval: 1000 * 5, // 5 seconds
  // Allow notification in npm scripts for easier testing
  shouldNotifyInNpmScript: true
});

// Check if an update is available and display a message
if (notifier.update) {
  console.log(`Update available: ${notifier.update.latest}. Current: ${notifier.update.current}`);
  notifier.notify();
} else {
  console.log('No update available or check not yet performed.');
}

// In a real CLI, this would typically run once at startup.

view raw JSON →