CLI Update Notifier
simple-update-notifier is a lightweight and straightforward utility designed to inform users of command-line interface (CLI) applications about available updates on npm. It works by checking the npm registry for newer versions of a specified package and, if an update is found, prints a non-intrusive notification to the terminal. The library is currently at version 2.0.0, released in June 2023, and maintains a fairly active release cadence with minor and patch updates for dependency management and minor enhancements, and less frequent major releases for breaking changes. A key differentiator is its simplicity and built-in caching mechanism, which prevents excessive API calls to npm by only checking for updates at a configurable interval (defaulting to once a day). It also includes options to control notification behavior in npm scripts and specify different npm `dist-tags`.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'name')
cause The `pkg` option was not provided or was an empty object, causing the notifier to attempt reading properties from an undefined or incomplete object.fixPass a valid object containing `name` and `version` properties to the `pkg` option, typically derived from your application's `package.json`. -
SyntaxError: Unexpected token 'assert'
cause You are using the `import ... assert { type: 'json' }` syntax for JSON modules but your Node.js version is too old (pre-16.15) or your environment does not support it, or your bundler isn't configured for it.fixUpgrade your Node.js environment to version 16.15 or higher. If using a bundler, ensure it's configured to handle JSON modules with import assertions. Alternatively, if your project is CommonJS, use `const packageJson = require('./package.json');`.
Warnings
- breaking Version 2.0.0 introduced a breaking change by raising the minimum required Node.js version. This was done to bump the `semver` dependency and avoid potential audit errors for users of the library.
- gotcha The `pkg` option is mandatory and must contain `name` and `version` properties. Forgetting to pass the `package.json` data will prevent the notifier from functioning correctly.
- gotcha Update notifications are disabled by default when running in an npm script context (e.g., `npm run my-script`). This prevents noisy output during build processes or CI/CD pipelines.
- gotcha When using ESM in Node.js, directly `import`ing `package.json` requires the `assert { type: 'json' }` clause. This is a Node.js-specific syntax for JSON modules.
Install
-
npm install simple-update-notifier -
yarn add simple-update-notifier -
pnpm add simple-update-notifier
Imports
- updateNotifier
const updateNotifier = require('simple-update-notifier');import updateNotifier from 'simple-update-notifier';
- packageJson
const packageJson = require('./package.json');import packageJson from './package.json' assert { type: 'json' }; - NotifierOptions
import { NotifierOptions } from 'simple-update-notifier';import type { NotifierOptions } from 'simple-update-notifier';
Quickstart
import updateNotifier from 'simple-update-notifier';
import packageJson from './package.json' assert { type: 'json' };
// In a real CLI application, this would typically be called once at startup.
// To demonstrate, we'll create a dummy package.json object if not found.
const pkgInfo = packageJson || {
name: 'my-cli-app',
version: '1.0.0',
};
async function main() {
console.log('Running CLI app...');
await updateNotifier({
pkg: pkgInfo,
updateCheckInterval: 1000 * 60 * 60, // Check hourly for demo purposes
shouldNotifyInNpmScript: true, // Allow notifications in npm scripts for testing
debug: process.env.DEBUG_UPDATE_NOTIFIER === 'true', // Enable debug logging with env var
});
console.log('CLI app finished.');
}
main();