Skip to content
ES

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.

Terminal window
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 dev

bun 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:

Terminal window
newt init newthub --monorepo
cd newthub
bunx newt add backend apps/api

--port, --db, --files and --cluster apply to the first backend, as in a plain newt init. See installation.

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 later

Each 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:

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 install at the root installs every backend.
  • bun run dev, test, typecheck and lint at the root run in every backend.
  • Inside a backend’s folder, the same scripts run for that backend only.
  • The root also holds overrides for the lint’s TypeScript, because Bun reads overrides only from the root.

From anywhere inside the monorepo:

Terminal window
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 backend takes --port, --db, --files and --cluster, with the same defaults as newt init.

Every newt add command works on one backend. newt picks it like this:

  1. With --backend <folder>, that backend.
  2. Inside a backend’s folder, that backend.
  3. At the root with only one backend, that one.
  4. Otherwise it stops and asks for --backend.
Terminal window
newt add context chat
Which backend? This monorepo has apps/admin, apps/api: add --backend apps/admin
Terminal window
newt add context chat --backend apps/api
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.

Terminal window
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.