Workbox CLI
workbox-cli is the command-line interface for the Workbox library, a set of JavaScript modules for adding offline support to web applications. It streamlines the process of generating service workers and precaching assets, simplifying the integration of Workbox into projects without requiring complex custom build scripts. The current stable version is 7.4.0. Workbox CLI's release cadence generally aligns with the broader Workbox project, seeing frequent patch updates for dependency maintenance and security, with major versions introducing significant changes like Node.js version bumps. Its key differentiator is providing a user-friendly entry point for common Workbox use cases through a series of interactive wizards and declarative configuration files, abstracting away the underlying `workbox-build` module for a smoother developer experience.
Common errors
-
Error: Minimum required Node.js version is 20.0.0. You are running v18.17.0.
cause Attempting to run `workbox-cli` version 7 or higher with an unsupported Node.js version.fixUpgrade Node.js to version 20.0.0 or newer. Use `nvm install 20 && nvm use 20` or update your system's Node.js installation. -
`workbox-cli: command not found`
cause The `workbox-cli` package is not installed globally or `npx` is not resolving the package correctly.fixEither install it globally (`npm install -g workbox-cli`) or use `npx workbox-cli <command>` to execute the latest version without global installation. -
Error: [workbox-build] 'globDirectory' must be a valid directory.
cause The `globDirectory` path specified in the `workbox-config.js` does not exist or is incorrect.fixVerify that the `globDirectory` property in your configuration file points to an actual, existing directory where your static assets are located.
Warnings
- breaking Workbox v7.0.0 introduced a breaking change by raising the minimum required Node.js version. Projects using older Node.js versions will encounter errors.
- gotcha The Workbox CLI undergoes regular dependency updates, some of which address critical vulnerabilities. Running outdated versions can expose your project to known security risks.
- gotcha Incorrectly configuring `globDirectory` or `swDest` in your Workbox configuration file can lead to service workers not being generated, or generated in an unexpected location, failing to serve assets correctly.
Install
-
npm install workbox-cli -
yarn add workbox-cli -
pnpm add workbox-cli
Imports
- workbox-cli (general invocation)
npx workbox-cli <command> [options]
- generateSW
import { generateSW } from 'workbox-cli'npx workbox-cli generateSW ./workbox-config.js
- injectManifest
import { injectManifest } from 'workbox-cli'npx workbox-cli injectManifest ./workbox-config.js
- wizard
import { wizard } from 'workbox-cli'npx workbox-cli wizard
Quickstart
/* workbox-config.js */
module.exports = {
globDirectory: 'dist/',
globPatterns: [
'**/*.{html,js,css,png,jpg,gif,svg,json,webmanifest}'
],
swDest: 'dist/sw.js',
clientsClaim: true,
skipWaiting: true,
navigateFallback: '/index.html',
runtimeCaching: [{
urlPattern: new RegExp('^https://fonts.(?:googleapis|gstatic).com/'),
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'google-fonts',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 30,
},
},
}],
};
// To run this example:
// 1. Create a directory named 'dist' in your project root.
// 2. Place some static files (e.g., index.html, style.css) into 'dist'.
// 3. Save the above content as 'workbox-config.js' in your project root.
// 4. Open your terminal in the project root and run:
// npx workbox-cli generateSW workbox-config.js
// This will generate 'dist/sw.js' based on your configuration.