Module Discovery Configuration
This guide covers advanced configuration for Minima.js module discovery. Use it when you need custom file names, a non-standard directory structure, or a custom scanner.
Default Behavior
By default, Minima.js:
- Scans the directory of your entry file
- Looks for
module.{ts,js,mjs}files - Treats a
module.tsin the root as the root module
typescript
// src/index.ts
import { createApp } from "@minimajs/server/bun";
const app = createApp(); // defaults: root = entry dir, index = "module"
await app.listen({ port: 3000 });Configuration Options
Configure discovery via createApp({ moduleDiscovery: { ... } }).
index (filename)
Use a different filename instead of module.ts.
typescript
// src/index.ts
import { createApp } from "@minimajs/server/bun";
const app = createApp({
moduleDiscovery: {
index: "route", // looks for route.ts
},
});Resulting pattern: **/route.{ts,js,mjs}
text
src/
├── index.ts
├── users/
│ └── route.ts
└── posts/
└── route.tsroot (directory)
Scan a specific directory instead of the entry file folder.
typescript
// src/index.ts
import { createApp } from "@minimajs/server/bun";
import path from "node:path";
const app = createApp({
moduleDiscovery: {
root: path.resolve(import.meta.dir, "features"),
},
});Resulting pattern: features/**/module.{ts,js,mjs}
text
src/
├── index.ts
└── features/
├── users/
│ └── module.ts
└── posts/
└── module.tsImportant:
moduleDiscovery.rootmust be an absolute path. Usepath.resolve().
Combine root + index
typescript
// src/index.ts
import { createApp } from "@minimajs/server/bun";
import path from "node:path";
const app = createApp({
moduleDiscovery: {
root: path.resolve(import.meta.dir, "app"),
index: "route",
},
});Resulting pattern: app/**/route.{ts,js,mjs}
Custom Scanner
You can provide a custom scanner to control how modules are discovered.
typescript
// src/index.ts
import { createApp, type ModuleScanner } from "@minimajs/server";
import { readdir } from "node:fs/promises";
import path from "node:path";
const scanner: ModuleScanner = async function* scan(dir, index) {
// Example: only scan one level deep
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const file = path.join(dir, entry.name, `${index}.ts`);
yield file;
}
};
const app = createApp({
moduleDiscovery: {
scanner,
},
});Use a custom scanner if you need:
- Non-standard folder layouts
- Filters based on naming conventions
- External module sources
Disabling Module Discovery
If you want to register everything manually:
typescript
const app = createApp({
moduleDiscovery: false,
});Troubleshooting
Modules not discovered? Check:
- ✅ Is the filename correct? (
module.tsby default) - ✅ Is
moduleDiscoveryenabled? (it is by default) - ✅ Is the root path absolute?
- ✅ Is the file inside the discovery root?