Docs for the next release (prerelease). Install it with npm i @meghjs/cli@next. Latest release docs →

Next release (prerelease) · Latest docs →

Skip to content

CLI ​

Install the CLI globally, or run it with npx:

sh
npm i -g @meghjs/cli

Command Overview ​

CommandAliasDescription
newCreate a new application
addAdd views, a Vite frontend or Inertia to an existing project
serveStart the dev server with watch + rebuild
buildCompile using tsconfig.build.json
make:modelm:mCreate a model: a plain class, a MeghORM model with --orm (columns from its migrations), or a TypeORM entity with --typeorm
sync:models:mSync an existing model's columns with its migrations (was update:model)
make:resourcem:rCreate a REST resource for a model: DTOs, CRUD service and controller
make:controllerm:cCreate a controller
make:servicem:sCreate a service
make:configm:cnfCreate a config class
make:middlewarem:mlwrCreate a middleware
make:mailm:mailCreate a Mailable in src/mail/
make:eventm:evCreate an event in src/events/
make:listenerm:lsnCreate an event listener in src/listeners/ (--event to type it)
make:queuem:qCreate a queue with a job handler in src/queues/
make:workerm:wCreate a worker for a queue in src/workers/
make:taskm:task, make:schedulerCreate a scheduled task in src/tasks/ (--cron, --interval)
make:dtom:dtoCreate a DTO in src/dtos/
make:classm:clsCreate a plain class in src/classes/
make:viewm:vCreate a .megh.html view (or -c component) in views/
make:pagem:pCreate an Inertia page (Vue, React or Svelte) in frontend/pages/
make:testm:tCreate a unit test (<name>.spec.ts) for an existing file
make:migrationm:migCreate a database migration
make:messagingCreate the migration for the messaging tables (outbox, scheduler locks, dedupe)
make:seederm:sdCreate a database seeder
make:factorym:fCreate a model factory with Faker values
migrateRun pending migrations (--seed to seed afterwards)
migrate:statusShow which migrations have run
migrate:rollbackUndo the last migration (--step <n> for more)
migrate:resetUndo every migration
migrate:refreshUndo every migration, then run them all again (--seed too)
db:seedRun the seeders (--seeder <name> for one)
messaging:failedList messages that failed after their retries, or retry / discard them. See Messaging
scaffold:domainsc:domainCreate the tables and models (or resources) of a business domain
scaffold:authsc:authAdd sign-up and sign-in with JWTs and/or API keys, optionally with roles
scaffold:rbacsc:rbacAdd roles and permissions to an app's users
template:listtemplatesList the model templates
make:agentsWrite AGENTS.md (and a CLAUDE.md that imports it) for coding agents

Every make:* command takes -f / --force to overwrite an existing file. The generators except make:model and the database ones (make:migration, make:messaging, make:seeder, make:factory) also write a unit test next to the file; pass --notest (or -nt) to skip it. See Tests. make:model and make:resource also build the table from a template with --template or --fields.

Pages ​

PageCommands
Generatorsmake:resource, make:controller, make:service, make:model, sync:model, make:middleware, make:config, make:mail, make:event, make:listener, make:queue, make:worker, make:task, make:dto, make:class, make:test
Database Commandsmake:migration, make:messaging, migrate*, make:seeder, db:seed, make:factory
Messagingmessaging:failed
Scaffold--template, --fields, scaffold:domain, scaffold:auth, scaffold:rbac, template:list

Version and info ​

sh
npx megh -v       # the CLI version, e.g. 0.7.3 (also --version)
npx megh --info   # the logo, CLI, Node.js and OS versions, and the project's Megh packages (also -i)

The logo appears only with --info; -v, --help and the other commands print without it. --info also shows the installed @meghjs/core, @meghjs/data and TypeScript versions when run inside an app: paste it into a bug report.

Application ​

sh
npx @meghjs/cli new myapi

You are prompted for a package manager and optional features: OpenAPI docs, CORS and MeghORM. With MeghORM you also pick the database. Add Mail, Scalar UI or the @meghjs/data plugin afterwards. Non-interactively:

sh
npx @meghjs/cli new myapi --yes --database postgres   # also: mysql, sqlite, mssql

--database (or the orm feature, which defaults to SQLite) installs the driver, adds app.useMeghORM() to src/app.ts, writes DATABASE_URL to .env and creates src/database/migrations/.

For a server-rendered app, pass --mvc:

sh
npx @meghjs/cli new myapp --mvc

It adds app.useViews() and app.useMethodOverride() to src/app.ts, a layout in views/layouts/app.megh.html, a page in views/pages/home.megh.html, and a HomeController that renders it with Results.View('pages.home', ...). Add more pages with make:view. See Views. --mvc can't be combined with --minimal.

For a single-page app with Inertia.js, pass --inertia and optionally the frontend framework (vue by default, react or svelte):

sh
npx @meghjs/cli@next new myapp --inertia     # Vue
npx @meghjs/cli@next new myapp --inertia react

It adds useViews(), useVite(), useSession() and useInertia() to src/app.ts, and:

  • views/app.megh.html: the HTML document, with @vite and @inertia
  • frontend/app.ts (app.tsx for React): the Inertia client entry
  • frontend/pages/Home.vue (.tsx, .svelte): the page HomeController returns with Results.Inertia('Home', ...)
  • frontend/css/app.css and frontend/tsconfig.json
  • vite.config.mts, which builds frontend/ into public/build
  • a random SESSION_SECRET in .env

megh serve builds the frontend too (see below), and npm run build runs vite build before megh build. Add pages with make:page. --inertia can't be combined with --minimal or --mvc.

--mvc and --inertia run the same steps as megh add, so a project created plain and upgraded later ends up the same.

Add to a project ​

Start small and add layers as the app grows. Each step only adds files and setup. It never moves your code:

sh
npx megh add views              # server-rendered .megh.html in views/
npx megh add vite               # browser code in frontend/, built by Vite
npx megh add inertia [vue|react|svelte]   # Inertia pages in frontend/pages/ (adds vite)
CommandAdds
add viewsviews/layouts/app.megh.html, useViews() and useMethodOverride()
add vitefrontend/ (app.ts, css/, tsconfig.json), vite.config.mts, useVite(), vite and @fastify/static, public/build/ in .gitignore. A layout gets @vite('frontend/app.ts')
add inertiaVite if missing, the framework's packages and Vite plugin, frontend/app.ts (the Inertia entry), frontend/pages/Home, views/app.megh.html, useViews(), useSession() (with SESSION_SECRET), useInertia()

Setup goes into src/app.ts before app.useControllers(...), or before the first route in a minimal app. The commands are safe to run again: what's already there is kept, and nothing changes the second time.

Files Megh generated, such as the plain frontend/app.ts and vite.config.mts from add vite, are replaced when you haven't edited them. A vite.config you changed is patched in place (plugin and entry added). A frontend/app.ts with your code is never overwritten: move it, or pass --force.

Changes to existing files are shown as a diff before they're kept. Pass -y to skip the prompt, or --dry-run to only see them. Run your package manager's install afterwards when dependencies were added.

The project also gets an AGENTS.md: instructions for coding agents such as Claude Code, Codex, Cursor and Copilot. It lists the project's commands, where code goes, and the Megh rules that compile cleanly but fail at runtime (register new controllers, give every DTO property a validator, and so on). A CLAUDE.md containing @AGENTS.md makes Claude Code read the same file. Edit both freely: they're yours. To add them to an existing project, run npx megh make:agents (--force replaces an existing AGENTS.md; a CLAUDE.md of your own is never replaced).

The new project is put under git with git init, unless git isn't installed or the folder is already inside a repository. Pass --skip-git (or set MEGH_SKIP_GIT=1) to skip it. Then:

sh
cd myapi
npm run start:dev

Development Server ​

sh
npx megh serve

Compiles src/ with SWC into dist/, starts the app and restarts it on change. The entry point comes from megh.entry in package.json, falling back to main.

When the project has a vite.config file (after megh add vite or add inertia), serve also starts Vite's dev server. Its output is prefixed [VITE]. Edits in frontend/ then reach the open page with hot module replacement: no reload, and component state is kept. See Vite.

OptionEffect
--no-hmrRun vite build --watch instead. Pages load the built files; reload to see changes
--no-viteDon't run Vite; run it yourself

A vite.config without the megh() plugin from @meghjs/core/vite gets vite build --watch, and serve says how to turn on HMR.

Build ​

sh
npx megh build

Removes the output folder (outDir, usually dist/) and compiles the project with tsconfig.build.json.

Released under the MIT License.