Skip to content

First Steps

This guide walks you through setting up your first next-js-backend application.

Prerequisites

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 dev
bash
npx next-js-backend new my-api
cd my-api
npm install
npm run dev

Your 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/bun
bash
npm install next-js-backend elysia reflect-metadata
npm install -D typescript @types/node
bash
pnpm add next-js-backend elysia reflect-metadata
pnpm add -D typescript @types/node

2. 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.ts
bash
npx tsx src/main.ts
bash
npx ts-node --esm src/main.ts

Visit http://localhost:3000 — you'll see the JSON response.

Released under the MIT License.