CLI Update Notifier (CommonJS)
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
-
TypeError: Cannot read properties of undefined (reading 'name')
cause The `pkg` object passed to `updateNotifier` is missing or does not have the required `name` or `version` properties.fixEnsure you are passing a valid `pkg` object, typically by reading your `package.json`: `const pkg = require('./package.json'); updateNotifier({pkg});` -
Update notification not appearing on first run or immediately after installing an update.
cause The library is designed to defer notifications based on `updateCheckInterval` (defaulting to 1 day) and to perform checks asynchronously in a child process to avoid impacting app startup performance. It also specifically waits out the interval before showing any notification, even if an update is available, to avoid annoying users on first install.fixFor development and testing, temporarily set `updateCheckInterval` to a very low value (e.g., `1000 * 5` for 5 seconds) in the options. Remember to run the application multiple times to trigger the stored notification. -
Notifications not showing up when my CLI is run from a `package.json` script (e.g., `npm run my-cli`).
cause The `shouldNotifyInNpmScript` option defaults to `false` to prevent unwanted output during automated tasks.fixSet `shouldNotifyInNpmScript: true` in the options passed to `updateNotifier`: `updateNotifier({ pkg, shouldNotifyInNpmScript: true }).notify();` -
ERR_REQUIRE_ESM: require() of ES Module [path/to/update-notifier-cjs] not supported.
cause Attempting to import `update-notifier-cjs` using ES module `import` syntax in an environment that enforces strict module resolution.fixThis package is strictly CommonJS. Use the `require()` syntax: `const updateNotifier = require('update-notifier-cjs');`. If your project is ESM-only, you might need to consider alternatives or dynamic `import()` if the context allows, though this package is built for CJS.
Warnings
- gotcha Update notifications are not shown on the very first run of a CLI application or if the `updateCheckInterval` has not elapsed. The check runs asynchronously in a detached child process, and results are persisted for subsequent runs. This behavior is designed to be non-intrusive but can be confusing during development/testing.
- gotcha The `options.pkg` parameter is mandatory and must contain valid `name` and `version` properties. Failing to provide a correctly structured `pkg` object will result in errors during initialization.
- gotcha The `notifier.notify()` method only displays the notification message if the current process is running in a TTY (interactive terminal) environment. Notifications will be silently skipped in non-interactive environments like CI/CD pipelines or automated scripts.
- gotcha By default, update notifications are suppressed when the CLI application is executed as part of an npm script (e.g., `npm run my-script`). This is controlled by the `shouldNotifyInNpmScript` option.
Install
-
npm install update-notifier-cjs -
yarn add update-notifier-cjs -
pnpm add update-notifier-cjs
Imports
- updateNotifier
import { updateNotifier } from 'update-notifier-cjs';const updateNotifier = require('update-notifier-cjs');
Quickstart
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.