First Steps
This guide walks you through setting up your first next-js-backend application.
Prerequisites
- Bun
>= 1.0— Install Bun or Node.js>= 20.0— nodejs.org - TypeScript
>= 5.0
Create a New Project
Use the CLI to scaffold a project instantly:
bash
npx next-js-backend new my-api
cd my-api
bun install
bun run devbash
npx next-js-backend new my-api
cd my-api
npm install
npm run devYour server is now running at http://localhost:3000 🚀
Manual Setup
1. Install dependencies
bash
bun add next-js-backend elysia reflect-metadata
bun add -d typescript @types/bunbash
npm install next-js-backend elysia reflect-metadata
npm install -D typescript @types/nodebash
pnpm add next-js-backend elysia reflect-metadata
pnpm add -D typescript @types/node2. Configure TypeScript
json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strict": true
}
}Required flags
experimentalDecorators and emitDecoratorMetadata are both required. The framework relies on TypeScript's decorator metadata emission (reflect-metadata) for the DI container to resolve constructor parameter types automatically.
3. Create your first module
typescript
import 'reflect-metadata';
import { ElysiaFactory } from 'next-js-backend';
import { AppModule } from './modules/app/app.module';
const app = await ElysiaFactory.create(AppModule);
app.listen(3000, () => {
Logger.log('🚀 Server running at http://localhost:3000');
});typescript
import { Module } from 'next-js-backend';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}typescript
import { Controller, Get } from 'next-js-backend';
import { AppService } from './app.service';
@Controller('/')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('/')
getHello() {
return this.appService.getHello();
}
}typescript
import { Injectable } from 'next-js-backend';
@Injectable()
export class AppService {
getHello() {
return { message: 'Hello from next-js-backend!' };
}
}4. Run
bash
bun run src/main.tsbash
npx tsx src/main.tsbash
npx ts-node --esm src/main.tsVisit http://localhost:3000 — you'll see the JSON response.
