1---2name: deno-project-templates3description: Use when scaffolding new Deno projects. Provides templates for Fresh web apps, CLI tools, libraries, and API servers with modern best practices.4license: MIT5metadata:6 author: denoland7 version: "1.2"8---910# Deno Project Templates1112This skill provides templates for creating new Deno projects with modern best practices.1314## When to Use This Skill1516- Creating a new Deno project from scratch17- Setting up project structure for different application types18- Scaffolding Fresh web apps, CLI tools, libraries, or API servers1920## Scope Boundaries2122This skill applies **only** when the user asks for a Deno project. Follow these rules:2324- If the user asks for a **Node.js, Python, Go, Rust, or other non-Deno project**, answer using that technology's project setup directly. Do not suggest Deno templates.25- Only use these templates when the user explicitly asks for a Deno project or is working in a Deno environment.26- When mentioning deprecated patterns, describe them generically. Do not write out deprecated URLs or import syntax — only show the correct modern approach.2728## Project Types2930Choose the appropriate template based on what you want to build:3132| Type | Use Case | Key Files |33|------|----------|-----------|34| **Fresh web app** | Full-stack web application with Fresh framework | `main.ts`, `routes/`, `islands/` |35| **CLI tool** | Command-line application | `main.ts` with arg parsing |36| **Library** | Reusable package to publish on JSR | `mod.ts`, `mod_test.ts` |37| **API server** | Backend API without frontend | `main.ts` with HTTP handlers |3839## Fresh Web App4041For full-stack web applications, use the Fresh initializer:4243```bash44deno run -Ar jsr:@fresh/init my-project45cd my-project46```4748This creates:49- `deno.json` - Project configuration and dependencies50- `main.ts` - Server entry point51- `client.ts` - Client entry point (CSS imports)52- `vite.config.ts` - Vite build configuration53- `routes/` - Pages and API routes (file-based routing)54- `islands/` - Interactive components that get JavaScript on the client55- `components/` - Server-only components (no JavaScript shipped)56- `static/` - Static assets like images, CSS5758**Development:** Fresh uses Vite. The dev server runs at `http://localhost:5173` (not port 8000).5960```bash61deno task dev62```6364## CLI Tool6566Create a command-line application with argument parsing.6768**Template files:** See `assets/cli-tool/` directory.6970### deno.json7172```json73{74 "name": "my-cli",75 "version": "0.1.0",76 "exports": "./main.ts",77 "tasks": {78 "dev": "deno run --allow-all main.ts",79 "compile": "deno compile --allow-all -o my-cli main.ts"80 },81 "imports": {82 "@std/cli": "jsr:@std/cli@^1",83 "@std/fmt": "jsr:@std/fmt@^1"84 }85}86```8788### main.ts8990```typescript91import { parseArgs } from "@std/cli/parse-args";92import { bold, green } from "@std/fmt/colors";9394const args = parseArgs(Deno.args, {95 boolean: ["help", "version"],96 alias: { h: "help", v: "version" },97});9899if (args.help) {100 console.log(`101${bold("my-cli")} - A Deno CLI tool102103${bold("USAGE:")}104 my-cli [OPTIONS]105106${bold("OPTIONS:")}107 -h, --help Show this help message108 -v, --version Show version109`);110 Deno.exit(0);111}112113if (args.version) {114 console.log("my-cli v0.1.0");115 Deno.exit(0);116}117118console.log(green("Hello from my-cli"));119```120121## Library122123Create a reusable package for publishing to JSR.124125**Template files:** See `assets/library/` directory.126127### deno.json128129```json130{131 "name": "@username/my-library",132 "version": "0.1.0",133 "exports": "./mod.ts",134 "tasks": {135 "test": "deno test",136 "check": "deno check mod.ts",137 "publish": "deno publish"138 }139}140```141142### mod.ts143144```typescript145/**146 * my-library - A Deno library147 *148 * @module149 */150151/**152 * Example function - replace with your library's functionality153 *154 * @param name The name to greet155 * @returns A greeting message156 *157 * @example158 * ```ts159 * import { greet } from "@username/my-library";160 * console.log(greet("World")); // "Hello, World"161 * ```162 */163export function greet(name: string): string {164 return `Hello, ${name}`;165}166```167168### mod_test.ts169170```typescript171import { assertEquals } from "jsr:@std/assert";172import { greet } from "./mod.ts";173174Deno.test("greet returns correct message", () => {175 assertEquals(greet("World"), "Hello, World");176});177```178179**Remember:** Replace `@username` with your JSR username before publishing.180181## API Server182183Create a backend API without a frontend.184185**Template files:** See `assets/api-server/` directory.186187### deno.json188189```json190{191 "tasks": {192 "dev": "deno run --watch --allow-net main.ts",193 "start": "deno run --allow-net main.ts"194 },195 "imports": {196 "@std/http": "jsr:@std/http@^1"197 }198}199```200201### main.ts202203```typescript204import { serve } from "@std/http";205206const handler = (request: Request): Response => {207 const url = new URL(request.url);208209 if (url.pathname === "/") {210 return new Response("Welcome to the API", {211 headers: { "Content-Type": "text/plain" },212 });213 }214215 if (url.pathname === "/api/hello") {216 return Response.json({ message: "Hello from Deno" });217 }218219 return new Response("Not Found", { status: 404 });220};221222console.log("Server running at http://localhost:8000");223serve(handler, { port: 8000 });224```225226## Post-Setup Steps227228After creating project files:229230```bash231cd my-project232deno install # Install dependencies233deno fmt # Format the code234deno lint # Check for issues235```236237## Development Commands by Project Type238239| Project Type | Start Development | Build/Compile |240|--------------|-------------------|---------------|241| Fresh | `deno task dev` (port 5173) | `deno task build` |242| CLI | `deno task dev` | `deno task compile` |243| Library | `deno test` | N/A |244| API | `deno task dev` | N/A |245246## Deployment247248When ready to deploy:249- Fresh: `deno task build && deno deploy --prod`250- CLI: `deno task compile` (creates standalone binary)251- Library: `deno publish` (publishes to JSR)252- API: `deno deploy --prod`253254## Best Practices255256- Always use `jsr:` imports for Deno packages (the old URL-based imports are deprecated)257- Run `deno fmt` and `deno lint` regularly258- Projects are configured for Deno Deploy compatibility