A KosmoJS project has four top-level directories, and each one has exactly one job. Once that clicks, the rest of the docs read much faster.
my-app/
├── package.json
├── .env -> env files shared by every folder
├── tsconfig.json -> covers everything outside src/, var/, dist/ and public/
│
├── src/ ✍️ YOU WRITE THIS
│ ├── front/ -> one source folder
│ └── admin/ -> another, fully independent
│
├── var/lib/ 🤖 DERIVED - never edit
│ ├── tsconfig.json -> base config the root tsconfig extends
│ ├── front/ -> derived code for src/front
│ └── admin/ -> derived code for src/admin
│
├── dist/ 📦 BUILD OUTPUT
│ ├── run.js -> runs every folder, one process
│ ├── front/
│ └── admin/
│
└── var/ 🗑️ Vite cache - disposableThe rule of thumb: you own src/, KosmoJS owns var/lib/. Every _/ import you write points into var/lib/.
Inside a Source Folder
A source folder is a self-contained app. This is one with a Hono backend, a React frontend with SSR enabled, and a sidecar - the fullest shape:
src/front/
├── kosmo.config.ts -> what this folder is
├── .env -> env files for this folder only
├── tsconfig.json -> extends ../../var/lib/front/tsconfig.json
├── index.html -> Vite's HTML entry
├── app.tsx -> global wrapper, wraps EVERY route
├── router.ts -> routerFactory: routes -> native router
│
├── api/ ── server side ───────────────────────────
│ ├── app.ts -> the backend app instance (appFactory)
│ ├── server.ts -> standalone server entry
│ ├── dev.ts -> dev hooks: requestHandler, teardownHandler
│ ├── errors.ts -> THE central error handler
│ ├── use.ts -> global middleware (runs for every route)
│ ├── env.d.ts -> global context/state types, custom UseSlots
│ └── users/
│ └── [id]/
│ ├── index.ts -> the route -> /api/users/:id
│ └── types.ts -> colocated helper, NOT a route
│
├── pages/ ── client side ───────────────────────────
│ ├── 404.tsx -> rendered for unmatched routes
│ ├── index/
│ │ ├── index.ts -> the route -> /
│ │ └── page.tsx -> renders it
│ └── users/
│ ├── layout.tsx -> wraps everything under /users
│ └── [id]/
│ ├── index.ts -> the route -> /users/:id
│ └── page.tsx -> renders it
│
├── components/
│ └── Link.tsx -> type-safe navigation component
│
├── types/ -> yours - nothing outside the names above is scanned
│ └── user.ts -> reached as `@/types/user`
│
├── entry/
│ ├── client.ts -> mount / hydrate in the browser
│ └── server.ts -> renderToString / renderToStream (SSR only)
│
└── sidecar/ ── standalone process ────────────────────
├── entry.ts -> the service: defineService({ start, teardown })
└── run.ts -> the runner: starts it, stops it on a signalThings worth noticing:
api/andpages/are siblings with parallel trees, so an endpoint and its page are always one folder apart. Neither directory name appears in a URL.- Only
index.tsis a route. Inpages/,page.*renders it andlayout.*wraps it; everything else in a route folder is a colocated helper. Rationale › app.*is not a layout. It sits at the folder root and wraps every route;layout.*files live insidepages/**/*and wrap a subtree. Details ›use.tsis not a route either. Drop one in anyapi/folder and it wraps that subtree. Details ›sidecar/is a process, not part of the app. It builds and runs on its own, andentry/beside it belongs to the frontend. Details ›- Everything else is yours.
index.ts,use.ts,page.*andlayout.*are the only names the scanner looks for, so any other file or directory -types/,server/,domain/- is ignored by it and reachable through@/. Where to put it is a judgement call, not a convention KosmoJS imposes. Suggestions ›
A folder with no backend simply has no api/; one with no frontend has no pages/, app.*, router.ts, index.html or entry/.
A folder with a sidecar has a sidecar/ directory beside the rest, holding entry.ts, run.ts and its tests. A folder with only a sidecar has just that, a kosmo.config.ts and a tsconfig.json.
Inside var/lib/
You never edit var/lib/, but knowing what lives there makes the _/ imports legible. This is a React + Hono folder; exact filenames vary by framework, while the _/ names you import stay the same:
var/lib/
├── tsconfig.json -> shared base for the root tsconfig
├── .gitignore -> keeps cache.json and types.ts, ignores the rest
└── front/
├── tsconfig.json -> this folder's jsxImportSource + path mappings
├── api.ts -> _/api defineRoute, use
├── api:factory.ts -> _/api:factory appFactory, errorHandlerFactory
├── api:dev.ts -> _/api:dev devSetup
├── app.tsx -> _/app the AppProvider seam
├── router.tsx -> _/router routerFactory, createRouters
├── query.ts -> _/query TanStack client (when enabled)
├── use.ts -> _/use framework hooks (Vue/Svelte/MDX)
├── entry/
│ ├── client.ts -> _/entry/client
│ └── server.ts -> _/entry/server
├── fetch/ -> _/fetch typed clients + ResponseT
└── @api/
└── routes.ts -> the RouteMapvar/lib/ has its own .gitignore - don't ignore it from the root
The root .gitignore ignores everything in var/ except var/lib/:
/var/*
!/var/lib/var/lib/ is left to var/lib/.gitignore, which handles it more precisely: it ignores everything except cache.json and types.ts at any depth.
So derived code is not committed - it is a build artifact. What is committed is the per-route derivation cache (cache.json, keyed by content hashes of the route file and its type dependencies), so a fresh clone or a CI run doesn't pay for a full rebuild.
Ignoring var/ or var/lib/ as a whole from the root would drop that cache, and make every clone slow. Git never reads a .gitignore inside an ignored directory, so the exceptions in var/lib/.gitignore would stop applying.
Deleting var/lib/ is safe but not free: it forces a full rebuild, which on a large project takes minutes. Details ›
Path Mappings
Five prefixes are reserved. Don't reuse them for your own aliases.
| Prefix | Resolves to | Use it for |
|---|---|---|
#/* | project root | anything shared across source folders - db layer, domain types |
#src/* | src/ | modules of any source folder, by name |
#lib/* | var/lib/ | derived code of any folder, by name |
@/* | this source folder | your own modules inside the folder |
_/* | var/lib/<this folder>/ | derived code - generated, never edited |
The three starting with # reach the same place from anywhere in the project; @/ and _/ resolve against the folder that imports them.
import { db } from "#/db"; // my-app/db.ts
import type { User } from "@/types/user"; // src/front/types/user.ts
import { defineRoute } from "_/api"; // var/lib/front/api.ts
import fetchClients from "_/fetch"; // var/lib/front/fetch/@/ and _/ are folder-relative: the same _/api in src/admin resolves to var/lib/admin/api.ts. That is what keeps folders isolated - admin's route names and navigation types never leak into front.
#src/ and #lib/ reach any folder by name, from anywhere - a source folder, or a file at the project root that belongs to no folder. #src/ gets a folder's own modules, #lib/ its derived code:
// a helper at the project root, building links into the admin folder
import { base } from "#lib/admin/core"; // var/lib/admin/core
// the admin folder, using the docs folder's route map
import { apiRouteMap } from "#lib/docs/core"; // var/lib/docs/core
// the admin folder, reusing a type the front folder keeps
import type { User } from "#src/front/types/user"; // src/front/types/user.tsInside a folder, @/types/user and #src/<this folder>/types/user are the same file, and so are _/core and #lib/<this folder>/core. Reach for @/ and _/ for your own folder, so it keeps working if the folder is renamed, and for #src/ and #lib/ when what you need belongs to another folder.
#/ is how folders share without packages. Put a type in db/models.ts, import it as #/db/models from every folder, change it once, and every folder sees it - no workspaces, no publishing, no version bumps.
Nothing outside src/<folder>/ is ever scanned for routes, so the layout there is entirely yours - these docs use db/ and shared/ in examples, nothing enforces either.
It is still typechecked: a folder's run follows its imports, so #/db/models is checked whenever a folder that imports it is. Adding it to the root include is worth doing anyway - that is what gives your editor a project for the file when you open it directly, and what covers code no folder imports yet.
Build Output
dist/front/
├── api/
│ ├── app.js -> the app instance, for custom mounting
│ └── server.js -> ready-to-run backend server
├── sidecar/ -> only when the folder declares one
│ ├── entry.js
│ └── run.js -> the runner, started with node
├── client/
│ ├── assets/ -> hashed JS, CSS, images
│ └── index.html
└── ssr/ -> only when SSR is enabled
├── app.js
├── server.js -> serves pages AND the API
├── assets/ -> hashed, served at <base>/assets/
└── public/ -> copy of public/, served at <base>/The simplest way to run all of this is node dist/run.js, which dispatches across every folder in one process. Details ›
Folders can also be deployed separately, in which case what you deploy depends on the folder's rendering mode - and for SSR folders it is not everything in the directory. Details ›
Multiple Folders
The point of the whole layout. Three apps, one install, one set of types/helpers:
src/
├── marketing/ MDX, no backend, base "/"
├── app/ React + Hono, base "/app"
└── admin/ Vue + H3, base "/admin"Each has its own kosmo.config.ts, its own routes, middleware, layouts and build. They share one package.json, one node_modules, and one #/ root.
pnpm dev # all folders
pnpm dev app # just one
pnpm build admin # build one, deploy it independently