Getting Started

Flux CLI

Add Flux to your app with one command. The CLI detects your setup, installs the packages, generates the engine, wires the service worker and lets you pick which pages work offline. It can also scaffold a brand new app, wire dynamic routes, print your database migration, check your setup and take Flux out again.

Prerequisites
  • Node 18+.
  • (Optional)An existing Next.js 14+ App Router project or a Vite + React project. With no project yet, the CLI creates one for you.
  • (Optional)A clean version control state, or run with --dry-run first. The CLI edits files in your project.
1

Install the CLI, or run it once

Read this first: flux-cli only works after the global install

Every command on this page is written as flux-cli <command>, for example flux-cli routes. That short name exists only after you install the CLI globally (below). If you have not, your terminal says flux-cli is not recognized. Without the install, put npx @tsworldtech/flux-cli@latest in front instead, for example npx @tsworldtech/flux-cli@latest routes.

Option A: install it once (recommended)

bash
npm install -g @tsworldtech/flux-cli
Then every flux-cli ... command on this page works from any folder. Open a new terminal after installing, then check it:
terminal
1# Confirm the command exists
2flux-cli --version
3
4# Update later
5npm install -g @tsworldtech/flux-cli@latest

Option B: run it without installing

bash
npx @tsworldtech/flux-cli@latest init
Nothing is installed on your machine, and @latest always runs the newest version. Use this for a one off setup, in CI, or from an AI coding agent. Add the command you want after @latest, for example routes or doctor.
terminal
1# These two are the same command
2
3# 1. After the global install
4flux-cli routes
5
6# 2. Without installing anything
7npx @tsworldtech/flux-cli@latest routes
Installed it but flux-cli is still not recognized?

Close the terminal and open a new one, because the list of installed commands is read when a terminal starts. If it still fails, the folder where your package manager puts global commands is not on your PATH. npm prefix -g prints the folder npm uses. With pnpm, run pnpm setup once and restart the terminal. Until you fix it, the npx form always works.

if flux-cli is not recognized
1# "flux-cli is not recognized" or "command not found" means the global
2# install has not happened, or this terminal has not picked it up yet.
3#
4# 1. Install it: npm install -g @tsworldtech/flux-cli
5# 2. Close the terminal and open a new one (the PATH is read at start)
6# 3. Check: flux-cli --version
7#
8# Or skip the install and run any command through npx:
9npx @tsworldtech/flux-cli@latest routes
Run it from the root of your app (or use --cwd in a monorepo). In a terminal it asks a few questions. Without a terminal, or with --yes, it never prompts, so it is safe for scripts and AI coding agents. The CLI has no license key.
Look before it changes anything

Add --dry-run to any command to print the full plan and change nothing. The plan lists every file it would create, edit or skip, and every package it would install. The questions are still asked, so you can see what each answer changes.

terminal
1# Show the full plan. Nothing is written, installed or changed.
2flux-cli init --dry-run
3
4# Same thing without the global install
5npx @tsworldtech/flux-cli@latest init --dry-run
2

Existing app or new app

You already have an app. The CLI reads package.json and detects Next.js (App Router, 14 or newer) or Vite with React. It never installs, upgrades or reinstalls your framework. It only adds the Flux packages, so your next, react and vite versions stay exactly as they are. Create React App is deprecated and not supported.
You have no app yet. Run it in an empty folder, or in any folder without a package.json, and it asks which framework you plan to use:
terminal
1Which framework do you plan to use?
2 1. Next.js (App Router)
3 2. React (Vite)
It then creates the app with the framework's own latest scaffolder (create-next-app@latest or create-vite@latest), installs it and continues with the normal setup. If the current folder is not empty it asks for a new folder name, so it never scaffolds over your files. To skip the questions:
terminal
1# Scaffold a new Next.js app called my-app, then set Flux up in it
2flux-cli init --framework next --name my-app
3
4# Same, but React with Vite, in JavaScript instead of TypeScript
5flux-cli init --framework vite --name my-app --js
6
7# No global install? Put npx in front of any of them:
8npx @tsworldtech/flux-cli@latest init --framework next --name my-app
3

What init changes

Everything init does appears in the plan before it runs. Every generated file starts with a marker comment, so re-running never overwrites a file you edited and changes nothing at all when nothing changed. A file with a shape the CLI cannot edit safely is left untouched and the exact snippet to paste is printed instead.
Next.js (App Router)Vite + React
Createslib/flux.ts, components/FluxClientWrapper.tsx, config/precache-routes.ts, app/offline/page.tsx (only if missing)src/lib/flux.ts, src/config/precache-routes.ts
Editsnext.config.* (wraps the config with withFlux), app/layout.* (wrapper, the flux-sw-init script and the manifest link in metadata)src/main.* (wraps <App /> in FluxProvider), vite.config.* (adds fluxVitePlugin), index.html (manifest link)
Bothpublic/manifest.json and placeholder icons in public/icons/. Skipped when a manifest already exists or with --no-manifest. Replace the icons with your own artwork.Same
OffersTo add public/sw.js to .gitignore, because it is regenerated on every build.Not applicable
Installs@tsworldtech/flux, @tsworldtech/flux-next, plus the adapter packages you choose@tsworldtech/flux, @tsworldtech/flux-react, plus the adapter packages you choose
Environment files are never touched

The CLI never reads, writes or creates any .env file. It prints variable names only, and the generated code references names, never values. It also never overwrites an existing file without --force, and package managers are started with an argument list, never a shell string.

4

Choose your adapter and routes with flags

In a terminal, init asks which backend you use and which pages should work offline. Every question has a flag, so you can answer them up front. --adapter takes supabase, sse, socketio, websocket or none, repeated or comma separated.
terminal
1# Supabase backend, cache every static page, no prompts
2flux-cli init --yes --adapter supabase --routes all-static
3
4# SSE backend with its endpoint, plus a hand picked route list
5flux-cli init --yes --adapter sse --url sse=/api/flux/stream --routes /,/pricing,/dashboard
6
7# More than one adapter, and choose which engine FluxProvider receives
8flux-cli init --yes --adapter supabase,sse --primary supabase
9
10# Static site with no backend at all
11flux-cli init --yes --adapter none --routes all-static
12
13# Skip the manifest and placeholder icons (you already have your own)
14flux-cli init --no-manifest
FlagWhat it does
--adapter <list>Realtime backends to wire: supabase, sse, socketio, websocket or none.
--url <name=url>Endpoint for a provider, for example --url sse=/api/flux/stream.
--primary <name>Which engine FluxProvider receives. Defaults to the first adapter.
--routes <list>Routes to cache offline, for example /,/pricing, or all-static.
--dynamic-prefixes <list>Vite only: URL prefixes of routes whose pages load data.
--no-manifestDo not create public/manifest.json, placeholder icons or the manifest link.
--framework next|viteOnly when no project exists. Creates the app first.
--name <folder>Only when no project exists. Folder for the new app.
--jsOnly when no project exists. JavaScript instead of TypeScript.
What --yes does when you give no --adapter

With --yes and no --adapter, every backend the CLI detects in your package.json is used. If it detects nothing, it exits with code 2 and lists the valid values. SSE and WebSocket cannot be detected from package.json, so they are never chosen for you.

5

Offline routes: routes, add-route, remove-route

Pages in your precache list are stored by the service worker so they open with no network. routes rescans your app and lets you pick pages. When you just want to change one or two, add-route and remove-route edit the list directly without scanning everything.

Rescan and choose

terminal
1# Rescan every page and pick which ones work offline
2flux-cli routes
3
4# Non interactive variants
5flux-cli routes --all-static
6flux-cli routes --add /pricing,/about
7flux-cli routes --remove /pricing

Add or remove specific routes

terminal
1# Add pages without scanning the whole app
2flux-cli add-route /pricing /about
3
4# Remove one
5flux-cli remove-route /pricing
6
7# Comma separated works too
8flux-cli add-route /pricing,/about
In a terminal, running either command with no routes asks for them. The edit is a small change to your list:
config/precache-routes.ts
1// config/precache-routes.ts (before and after flux-cli add-route /pricing)
2export const PRECACHE_ROUTES: string[] = [
3 '/',
4 '/dashboard',
5 '/offline',
6 '/pricing', // added
7]
RuleWhy
add-route checks that the page exists.A typo would otherwise precache a 404.
Dynamic routes such as /blog/[slug] are refused.They need a shell page and data lookup, which is what add-dynamic sets up (Step 6).
Pages that read cookies or headers are refused unless you pass --force.A cached copy of a per user page can show the wrong user's content, or stale content.
Pages whose route name looks private are refused unless you pass --force: any segment named portal, dashboard, account, admin, billing, checkout, payment, login, signup, profile or settings (and similar).These pages usually show signed in or payment content. The same rule leaves them unchecked in the routes list and out of --routes all-static.
/offline is always kept.It is the fallback when both cache and network are unreachable.
terminal
1# Cache a page even though it reads cookies or headers,
2# or has a private looking name
3flux-cli add-route /account --force
Do not force pages that show personal data

--force is for pages that read a cookie or have a private sounding name but render the same thing for everyone, such as a theme or language cookie, or a public page that happens to live under a folder called settings. If the page shows account data, billing or anything tied to a signed in user, keep it out of the precache list and let Flux cache that data per user instead.

6

Dynamic routes: add-dynamic

A dynamic route such as /blog/[slug] has infinitely many URLs, so it cannot be precached page by page. Flux precaches one shell document per route and fills it from IndexedDB. add-dynamic wires all of that for one route in a single command.

Next.js

terminal
1# Next.js: the route is written exactly as the folder is named
2flux-cli add-dynamic --route /blog/[slug] --channel blog_posts
3
4# Give the idbKey directly instead of a channel name
5flux-cli add-dynamic --route /projects/[id] --idb-key project_detail
6
7# The URL slug is a public slug but the detail entry is keyed by a UUID:
8# add the list channel as a fallback and say which fields hold the slug
9flux-cli add-dynamic --route /projects/[id] --idb-key project_detail --list-key projects:list --list-fields projectId,id
It finds the shell subpaths from your folders (for example the /settings and /activity pages under app/projects/[id]/), then makes three changes. It edits withFlux in next.config.*, merging dynamicRoutePrefixes, dynamicRouteShapes and dynamicRouteIdbKeys without duplicates. It adds the build time export to every page of the route. And it creates a client view stub in components/.
next.config.ts
1// next.config.ts (after flux-cli add-dynamic --route /blog/[slug] --channel blog_posts)
2export default withFlux({
3 // ...your existing options
4 dynamicRoutePrefixes: ['/blog/'],
5 dynamicRouteShapes: [
6 { prefix: '/blog/', subpaths: [''] },
7 ],
8 dynamicRouteIdbKeys: {
9 // idbKey of the blog_posts channel, looked up from your flux.register() call
10 '/blog/': 'blog_post_detail',
11 },
12})(nextConfig)
app/blog/[slug]/page.tsx
1// app/blog/[slug]/page.tsx (build time export added by the CLI)
2import { fluxDynamicRoute } from '@tsworldtech/flux-next/server'
3export const { generateStaticParams, dynamicParams } = fluxDynamicRoute
app/blog/[slug]/page.tsx
1// app/blog/[slug]/page.tsx (when Cache Components is on in next.config)
2//
3// Next.js refuses an empty generateStaticParams with Cache Components, so
4// the CLI uses a helper that returns one sentinel shell param instead.
5import { generateFluxShellParams } from '@tsworldtech/flux-next/server'
6export const generateStaticParams = () => generateFluxShellParams('slug')
components/BlogDetailView.tsx
1// components/BlogDetailView.tsx (stub created by the CLI, edit freely)
2//
3// Shows a skeleton while the page is the offline shell, waits for Flux to
4// finish loading, and only then calls notFound(). Fill in your real UI.
FlagWhat it does
--route <path>Next.js: /blog/[slug]. Vite: /blog/:id. Asked, or inferred when you only have one dynamic route.
--channel <name>The registered channel that holds the detail data. Its idbKey is looked up from your flux.register() calls.
--idb-key <key>Give the idbKey directly instead of a channel name.
--list-key <key>Optional idbKey of the whole list channel, used when the URL slug is not the detail key.
--list-fields <list>Fields of that list that match the URL slug. Defaults to the segment name and id.
Which export does it add?

With Cache Components on, Next.js refuses an empty generateStaticParams, so pages use generateFluxShellParams(), which returns one sentinel shell param. Otherwise they use fluxDynamicRoute. Shell routes are never added to precacheRoutes, they are generated from dynamicRouteShapes. Pages that are 'use client', or that already export generateStaticParams, are left alone and the exact snippet is printed.

Vite

terminal
1# Vite: React Router style path
2flux-cli add-dynamic --route /projects/:id
Vite adds dynamicRoutePrefixes only. It warns if build.manifest is not true in your Vite config. Running the command again reports "Already wired" and changes nothing.
7

Your database: sql

sql prints the Supabase migration for the tables you name. It never connects to a database, so you review the SQL and run it yourself. The shared parts are the canonical Flux schema: the changelog table, owner functions, the trigger, the delta and reconcile functions and locked down grants. Only the table specific parts are generated for your tables. See the Supabase and Postgres guide for what each part does.
terminal
1# Tables that belong to the signed in user (owner column: user_id)
2flux-cli sql projects:user
3
4# Owner is stored in another column. Use id when the row IS the user's own record.
5flux-cli sql profiles:user:id
6
7# Same rows for everyone
8flux-cli sql blog_posts:global
9
10# Owner found on a parent table
11flux-cli sql project_domains:parent:projects:project_id:user_id
12
13# Several tables at once
14flux-cli sql projects:user profiles:user:id blog_posts:global
Table specMeaning
table:globalThe same rows for everyone.
table:userEach row belongs to one user, owner stored in user_id.
table:user:columnOwner stored in another column. Use id when the row is the user's own record.
table:parent:parentTable:fkColumn:ownerColumnThe owner is found on a parent table through a foreign key.
terminal
1# Print to the terminal (default). Warnings appear as SQL comments at the top.
2flux-cli sql projects:user blog_posts:global
3
4# Write a migration file. Never overwrites an existing file.
5flux-cli sql projects:user --out supabase/migrations/20260101000000_flux_sync.sql
6
7# Include the retention function (the pg_cron schedule stays commented out)
8flux-cli sql projects:user --cleanup
9
10# Let logged out visitors catch up on global tables
11flux-cli sql blog_posts:global --allow-anon-global
FlagWhat it does
--out <file>Write the migration to a file. Never overwrites. In a terminal with supabase/migrations present, it offers to write a timestamped file there.
--cleanupInclude the retention function. The pg_cron schedule is left commented out.
--allow-anon-globalLet logged out visitors catch up on global tables.
Safe to run more than once

The migration is idempotent, so running it again is safe. Table and column names are validated as plain identifiers before they reach the SQL. Warnings are written as comments at the top of the output.

Socket.io, SSE and WebSocket projects

sql is for the Supabase adapter. If your project uses a Socket.io, SSE or WebSocket adapter, it prints a pointer to the matching guide instead, because those setups also need the bridge process that watches the database.

8

Check your setup: doctor

doctor checks packages, versions, storage prefixes, the wrapper, the service worker script, routes and the manifest. It also reads your own Flux code and reports problems that otherwise fail silently. All of it is static: nothing runs and nothing reads environment files.
terminal
1# Run every check
2flux-cli doctor
3
4# Treat warnings as failures (useful in CI)
5flux-cli doctor --strict
6
7# Machine readable report
8flux-cli doctor --json
what doctor reports
1# What doctor catches in your own Flux code
2register() missing store, channel, event, idbKey or ttl
3invalid ttl, scope or ingestionType
4the same channel registered twice, or two registrations sharing an idbKey
5per user looking data (billing, profile, account) without scope: 'user'
6createFlux() called outside lib/flux
7FluxProvider rendered more than once
8an engine cached under one storagePrefix but created with another
9cached routes that read cookies or headers
10a wrapper without "use client"
11a flux-sw-init script that is not beforeInteractive
12a dynamic route page with no build time export
13a dynamic route with no dynamicRouteShapes entry
Use it in CI

--strict makes warnings fail the run, so a drifting storage prefix or a missing scope: 'user' is caught in a pull request instead of in production.

9

Take Flux out again: remove

remove undoes what init did, and nothing else. It deletes the generated files you did not edit, unwraps withFlux, the layout wrapper and script, FluxProvider and the Vite plugin, removes the manifest and placeholder icons (only when they are still exactly what init wrote), removes the .gitignore entry and public/sw.js, and uninstalls the @tsworldtech/flux* packages.
terminal
1# See what would be removed. Nothing changes.
2flux-cli remove --dry-run
3
4# Do it
5flux-cli remove --yes
6
7# Keep the packages installed, remove everything else
8flux-cli remove --yes --keep-packages
9
10# Also delete generated files you edited
11flux-cli remove --yes --force
SituationWhat the CLI does
A generated file you editedKept. Use --force to delete it.
A file other code of yours still importsKept, so your app keeps building.
A file with a shape that cannot be edited safelyPrints exactly what to delete by hand and keeps the generated files until you rerun it.
zustand and the Supabase packagesLeft installed, because other code may use them.
Environment variables and .env filesNever touched.
No terminal and no --yesRefuses and exits with code 2.
10

Flags, exit codes and agents

These flags work on every command:
FlagWhat it does
--cwd <dir>Run in another directory. Use this in a monorepo.
--yes, -yNever prompt. Missing information is an error, not a hang.
--dry-runShow the plan and change nothing.
--forceOverwrite files the CLI generated earlier and you edited.
--jsonPrint one machine readable JSON report on stdout.
--help, -hShow help.
--version, -vShow the version.
terminal
1# Run against one app inside a monorepo
2flux-cli init --cwd apps/web
3flux-cli doctor --cwd apps/web

Exit codes

CodeMeaning
0Success.
1Failure.
2Invalid input, ambiguity or an unsupported project.

Using it from an AI coding agent or a script

terminal
1# Existing app, no prompts, one JSON report on stdout
2npx @tsworldtech/flux-cli@latest init --yes --adapter supabase --routes all-static --json
3
4# Brand new app
5npx @tsworldtech/flux-cli@latest init --yes --framework next --name my-app --adapter sse --json
6
7# Check afterwards, fail the run on warnings
8npx @tsworldtech/flux-cli@latest doctor --strict --json
A prompt is never shown without a terminal or with --yes. Missing information is an error with a clear message, never a hang. --json prints one report with the framework, package manager, engines, packages with requested and resolved versions, files created, edited and skipped (with reasons), environment variable names, snippets to paste and warnings.
flux-cli --help
1flux-cli <command> [flags]
2
3Commands
4 init Detect, install, generate, choose routes. Creates the app first when none exists
5 routes Rescan pages and change the offline route list
6 add-route Add routes to the offline list
7 remove-route Remove routes from the offline list
8 add-dynamic Wire a dynamic route so it works offline
9 sql Print the Supabase migration for the tables you name
10 doctor Static checks of packages, generated files and your Flux code
11 remove Take Flux out of the project again
12
13Global flags
14 --cwd <dir> Run in another directory (use this in a monorepo)
15 --yes, -y Never prompt. Missing information is an error, not a hang
16 --dry-run Show the plan and change nothing
17 --force Overwrite files the CLI generated earlier and you edited
18 --json Print one machine readable JSON report on stdout
19 --help, -h Show this help
20 --version, -v Show the version
11

License key and testing offline

The CLI itself is free and needs no key. It prints the names of the environment variables your setup uses (for example your Supabase URL and key), and you add them yourself. The one that belongs to Flux is the license token:
environment variable names
1# The CLI prints variable names only. It never reads, writes or creates .env files.
2# Add them yourself where your framework loads environment variables.
3
4FLUX_LICENSE_TOKEN= # only for a custom domain in production
When do you need FLUX_LICENSE_TOKEN?

Only for your own custom domain in production. In development, and on free Vercel or Netlify domains, every feature is available without a license key.

Then check that offline really works:
testing offline
1# The service worker does not serve from cache on localhost.
2# Test offline behaviour on an HTTPS origin that is not localhost:
3# - a deployed preview, or
4# - an HTTPS tunnel to your dev server.
5#
6# On localhost you can still confirm the caches filled:
7# DevTools > Application > Cache Storage
What the CLI does and does not do

It reads package.json, the lockfile, config files, layouts and route files, and writes only the files listed in the plan. The only network it uses is the npm registry. It has no telemetry, no install scripts and no runtime dependencies, because the code parser it uses to edit your files is compiled into the published file.

It does not install or upgrade your framework, does not touch .env files, does not connect to your database, and does not overwrite a file without --force. For the full manual setup it automates, see the Next.js guide or the React guide.

Powered by Flux