Guides
Monorepos
Keep several newt backends in one repository, each in its own folder.
A monorepo is one repository that holds several packages. With newt, each backend is a package of its own (a Bun workspace), and the root runs the scripts of all of them.
Use one when you run more than one backend, such as the Newthub API and an admin backend. One backend with several contexts does not need it.
Create one
Section titled “Create one”newt init newthub --monorepo --backend apps/api created newthub/package.json created newthub/AGENTS.md created newthub/.gitignore created newthub/CLAUDE.md created newthub/apps/api/package.json created newthub/apps/api/tsconfig.json created newthub/apps/api/eslint.config.ts created newthub/apps/api/AGENTS.md created newthub/apps/api/src/backend.ts created newthub/apps/api/src/stores.ts created newthub/apps/api/src/env.ts created newthub/apps/api/src/backend.test.ts created newthub/apps/api/src/contexts.ts created newthub/apps/api/src/index.ts created newthub/apps/api/src/auth.ts created newthub/apps/api/.env created newthub/apps/api/.env.example created newthub/apps/api/.gitignore created newthub/apps/api/CLAUDE.md
Next steps: cd newthub bun install bun run devbun create newt newthub --monorepo --backend apps/api does the same. Without a name, in a terminal, newt init asks whether you want a backend or a monorepo, and for a monorepo the name of its first backend, which goes in apps/. Answer none for an empty one.
Without --backend, the monorepo is empty, and its workspaces take apps/. Add the backends later with newt add backend:
newt init newthub --monorepocd newthubbunx newt add backend apps/api--port, --db, --files and --cluster apply to the first backend, as in a plain newt init. See installation.
The layout
Section titled “The layout”newthub/├── package.json workspaces and scripts for all backends├── AGENTS.md├── CLAUDE.md├── .gitignore└── apps/ ├── api/ a full newt backend │ ├── package.json │ ├── .env │ ├── src/ │ └── … └── admin/ another one, added laterEach backend folder is the same as a project made by newt init: its own package.json, .env, src/ and data/. See project structure.
The root package.json:
{ "name": "newthub", "private": true, "workspaces": ["apps/*"], "scripts": { "dev": "bun run --filter '*' dev", "test": "bun run --filter '*' test", "typecheck": "bun run --filter '*' typecheck", "lint": "bun run --filter '*' lint" }, "devDependencies": { "@newt-js/cli": "^0.0.0", "typescript": "7.0.2" }}bun installat the root installs every backend.bun run dev,test,typecheckandlintat the root run in every backend.- Inside a backend’s folder, the same scripts run for that backend only.
- The root also holds
overridesfor the lint’s TypeScript, because Bun reads overrides only from the root.
Add a backend
Section titled “Add a backend”From anywhere inside the monorepo:
newt add backend apps/admin skipped package.json (up to date) created apps/admin/package.json created apps/admin/tsconfig.json … created apps/admin/CLAUDE.md
Next steps: bun install cd apps/admin bun run dev- The folder’s name becomes the package name, here
admin. Two packages cannot share a name. - If no workspace pattern covers the folder, newt adds one to the root
package.json. - Each new backend gets the next free port: 10 above the highest one in use. The second backend serves on
3010. newt add backendtakes--port,--db,--filesand--cluster, with the same defaults asnewt init.
Run newt in a monorepo
Section titled “Run newt in a monorepo”Every newt add command works on one backend. newt picks it like this:
- With
--backend <folder>, that backend. - Inside a backend’s folder, that backend.
- At the root with only one backend, that one.
- Otherwise it stops and asks for
--backend.
newt add context chatWhich backend? This monorepo has apps/admin, apps/api: add --backend apps/adminnewt add context chat --backend apps/apiWhat newt refuses
Section titled “What newt refuses”| You run | newt says |
|---|---|
newt init with --backend but no --monorepo |
add --monorepo |
newt init inside a monorepo |
use newt add backend |
newt init --monorepo in a backend’s folder |
a monorepo needs a folder of its own |
newt add backend outside a monorepo |
use newt init |
newt add backend in a folder that is another package |
use a folder of its own |
To turn an existing backend into a monorepo, create the monorepo in a new folder and move the backend into it by hand.
Options
Section titled “Options”newt init [name] --monorepo [--backend <folder>] [--port <port>] \ [--db <database>] [--files <storage>] [--cluster <cluster>]newt add backend <folder> [--port <port>] [--db <database>] \ [--files <storage>] [--cluster <cluster>]See the newt init and newt add backend references.