CLI tool to update caniuse-lite to refresh target browsers from Browserslist config
npx @tessl/cli install tessl/npm-update-browserslist-db@1.1.0Update Browserslist DB is a CLI tool that updates the caniuse-lite database to refresh target browsers from Browserslist configuration. It automatically detects package managers (npm, yarn, pnpm, bun) and updates the caniuse-lite dependency to ensure browser queries use the most current browser data, reducing unnecessary polyfills and improving website performance.
npm install update-browserslist-db or npx update-browserslist-db@latestconst updateDb = require("update-browserslist-db");For TypeScript:
import updateDb = require("update-browserslist-db");
// or with type annotations
const updateDb: (print?: (str: string) => void) => void = require("update-browserslist-db");# Update caniuse-lite database
npx update-browserslist-db@latest
# Show help
npx update-browserslist-db --help
npx update-browserslist-db -h
# Show version
npx update-browserslist-db --version
npx update-browserslist-db -vconst updateDb = require("update-browserslist-db");
// Use default stdout output
updateDb();
// Custom output handling
updateDb((message) => {
console.log("Custom:", message);
});The package consists of several key components:
updateDb() orchestrates the entire update processUpdates the caniuse-lite database to the latest version, ensuring up-to-date browser data for tools like Autoprefixer and Babel.
/**
* Run update and print output to terminal.
* @param {function} [print] - Optional function to handle output strings
*/
function updateDb(print?: (str: string) => void): voidThe function:
Usage Examples:
const updateDb = require("update-browserslist-db");
// Basic usage with default output
updateDb();
// Custom output handling for logging
updateDb((message) => {
// Strip ANSI color codes if needed
const cleanMessage = message.replace(/\x1b\[\d+m/g, '');
logger.info('Browserslist update:', cleanMessage);
});
// Capture output for processing
let outputBuffer = '';
updateDb((message) => {
outputBuffer += message;
});Custom error class for browserslist update-specific errors. This error is thrown by the updateDb() function when update-specific issues occur.
/**
* Custom error class for browserslist update-specific errors
* @param {string} message - Error message
*/
function BrowserslistUpdateError(message: string): BrowserslistUpdateError
// Error instance properties:
interface BrowserslistUpdateErrorInstance extends Error {
name: "BrowserslistUpdateError";
message: string; // Descriptive error message
browserslist: true; // Marker property to identify browserslist errors
}Common error scenarios:
Usage Example:
const updateDb = require("update-browserslist-db");
try {
updateDb();
} catch (error) {
if (error.name === 'BrowserslistUpdateError') {
console.error('Browserslist update failed:', error.message);
// Handle browserslist-specific errors
} else {
throw error; // Re-throw other errors
}
}The tool automatically detects and supports:
package-lock.json or npm-shrinkwrap.jsonyarn.lock (manual lockfile update)yarn.lock (uses yarn up -R caniuse-lite)pnpm-lock.yaml (uses pnpm up caniuse-lite)bun.lock or bun.lockb (uses bun update caniuse-lite)The tool handles several error conditions gracefully:
npx update-browserslist-db in?"The following data structures are used internally by the package:
// Output handler function type
// function(str: string) => void
// Package manager detection result
// {
// file: string, // Path to lockfile
// mode: string, // "npm" | "yarn" | "pnpm" | "bun"
// content?: string, // Lockfile content (when loaded)
// version?: number // Yarn lockfile version (1 or 2)
// }
// Latest package version information
// {
// version: string, // Latest version number
// dist: {
// tarball: string, // Download URL
// integrity?: string // SRI hash (optional)
// }
// }
// Browser list data structure
// {
// [browser: string]: string[] // Browser name -> array of versions
// }The package includes complete TypeScript definitions:
/**
* Run update and print output to terminal.
*/
declare function updateDb(print?: (str: string) => void): void
export = updateDb