Each source folder in KosmoJS builds independently, producing deployment-ready output for that specific concern.
βΆοΈ Build Command β
Build all source folders for production:
pnpm buildBuild a specific source folder for production:
pnpm build @frontReplace @front with your source folder name (@admin, @marketing, etc.).
π¦ What Gets Built β
When you run pnpm build, KosmoJS produces:
Frontend assets:
- Optimized, bundled client code
- CSS, images, and other static assets
- Chunked and tree-shaken for minimal size
API server:
- Bundled Node.js application at
dist/SOURCE_FOLDER/api/index.js - All routes, middleware, and dependencies bundled together
- Ready to run with Node.js
SSR Bundle:
When SSR is enabled, the build process also generates a production-ready SSR bundle at dist/SOURCE_FOLDER/ssr/index.js. This standalone Node.js server is ready to deploy for server-side rendering.
π Build Output Structure β
dist/
βββ @src
βββ api
βΒ Β βββ index.js # Bundled API server
βββ client
βΒ Β βββ assets/ # Scripts, Styles, Images etc.
βΒ Β βββ index.html # Entry point
βββ ssr
βββ index.js # SSR Bundle (if enabled)π Running the Production Build β
Deploy the dist/SOURCE_FOLDER directory and run:
node dist/@front/api/index.jsThe API server is a standard Node.js ESM module. Deploy it to any Node.js environment - traditional servers, containers, serverless platforms, or edge runtimes.
ποΈ Building Multiple Source Folders β
You can build all folders at once by simply omitting the source folder name:
pnpm buildThis builds all your source folders in parallel, placing assets in the dist directory.
βοΈ Build Configuration β
API builds use the esbuild.json configuration at your project root:
{
"bundle": true,
"platform": "node",
"target": "node22",
"format": "esm",
"packages": "external",
"sourcemap": "linked",
"logLevel": "info"
}Customization options:
target- Node.js version (e.g.,node20,node22)sourcemap- Source map type (linked,inline,false)logLevel- Build verbosity (info,warning,error,silent)
Important: The bundle: true option is enforced for production builds, ensuring your API is bundled into a single executable file.
π Deployment Strategies β
Independent Deployment β
Deploy each source folder to its own environment:
# Deploy customer app
pnpm build @front
deploy dist/@front β app.example.com
# Deploy admin panel
pnpm build @admin
deploy dist/@admin β admin.example.comBenefits:
- Scale concerns independently
- Deploy updates without rebuilding everything
- Different teams can own different deployments
Unified Deployment β
Deploy all source folders to the same server with different base URLs:
# Build everything
pnpm build
# Deploy to single server
deploy dist/ β example.comConfigure nginx/caddy to route:
/β@frontassets/adminβ@adminassets/apiβ API server
Deployment Environments β
The bundled output works on:
- β Traditional servers - VPS, dedicated servers
- β Containers - Docker, Kubernetes
- β Serverless - AWS Lambda, Google Cloud Functions (with adapter)
- β Edge runtimes - Cloudflare Workers, Deno Deploy (with adapter)
- β PaaS - Heroku, Railway, Render
The standard Node.js output ensures portability across platforms.
π‘ Production Best Practices β
Test builds locally before deploying:
pnpm build
node dist/SOURCE_FOLDER/api/index.js -p 3000
# Test at localhost:3000Use environment variables for configuration:
- Database connection strings
- API keys and secrets
- Feature flags
- Service endpoints
Never hardcode credentials in your source code.
Enable source maps for debugging in production:
{
"sourcemap": "linked"
}Source maps help debug production errors but increase bundle size slightly. Consider the tradeoff for your use case.
Review bundle size periodically:
pnpm build
# Check dist/SOURCE_FOLDER/api/index.js sizeIf the bundle grows significantly, review dependencies and consider marking some as external.
β οΈ Troubleshooting β
Build fails?
- Check
esbuild.jsonsyntax - Verify all imports are resolvable
- Review build terminal output for errors
API crashes on startup?
- Verify environment variables are set
- Check Node.js version matches
targetinesbuild.json - Test database/service connections