Custom Preset

Custom local preset support is an experimental feature.

If you want to use a provider that Nitro doesn't support, or want to modify an existing one, you can create a local custom preset in your project.

Custom presets are local files that have a preset entry that defines builder configuration and a runtime entry point.

Example

Check unjs/nitro-preset-starter for a ready-to-use template.

First, we have to define our preset entry point in a local directory preset/nitro.config.ts

./preset/nitro.config.ts
import type { NitroPreset } from "nitropack";import { fileURLToPath } from "node:url"export default <NitroPreset>{  // extends: "node-server", // You can extend existing presets  entry: fileURLToPath(new URL("./entry.ts", import.meta.url)),  hooks: {    compiled() {      // ...    },  },};

The entry point will be used by your server or provider, and you can fully customize its behavior.

preset/entry.ts (Workers)
import "#internal/nitro/virtual/polyfill";const nitroApp = useNitroApp();export default {  fetch(request: Request) {    const url = new URL(request.url);    return nitroApp.localFetch(url.pathname + url.search, {      context: {},      host: url.hostname,      protocol: url.protocol,      method: request.method,      headers: request.headers,      body: undefined,    });  },};
preset/entry.ts (Node.js)
import "#internal/nitro/virtual/polyfill";import { Server } from "node:http";import { toNodeListener } from "h3";const nitroApp = useNitroApp();const server = new Server(toNodeListener(nitroApp.h3App));// @ts-ignoreserver.listen(3000, (err) => {  if (err) {    console.error(err);    process.exit(1);  }  console.log(`Listening on http://localhost:3000 (custom preset)`);});

Then in your nitro config file, you can use your custom preset.

nitro.config.ts
export default defineNitroConfig({  preset: "./preset",});
nuxt.config.ts
export default defineNuxtConfig({  nitro: {    preset: "./preset",  }});

Refer to the Nitro source code directly to have a better understanding of presets and entry points.