DIYgod/RSSHub · error · Error
directoryImport is not available in Worker builds
Error message
directoryImport is not available in Worker builds
What it means
directory-import.worker.ts is a build-time shim that replaces the real directory-import module in Worker bundles. Because Workers cannot perform filesystem directory scans at runtime, the shim throws immediately to make any accidental call visible. Worker builds are expected to use pre-built route lists (routes-worker.js) instead.
Source
Thrown at lib/utils/directory-import.worker.ts:13
// No-op shim for directory-import in Cloudflare Workers
// directoryImport is only used in dev mode, Worker builds use pre-built routes
export type DirectoryImportOptions = {
targetDirectoryPath: string;
importPattern?: RegExp;
includeSubdirectories?: boolean;
};
export const directoryImport = (_options: DirectoryImportOptions): Record<string, unknown> => {
// This should never be called in Worker builds
// Worker builds use pre-built routes from routes-worker.js
throw new Error('directoryImport is not available in Worker builds');
};
View on GitHub (pinned to bed535e087)
Solutions
- Ensure directoryImport is only referenced from the Node/dev entry, never from the Worker entry.
- Pre-build the route list at build time and import the generated module in the Worker bundle instead.
- Mark the offending import as external or exclude it via the Worker bundler config.
Example fix
// before (Worker bundle)
const routes = directoryImport({ targetDirectoryPath: './routes' });
// after
import routes from './generated/routes-worker.js'; Defensive patterns
Strategy: try-catch
Validate before calling
function isWorkerBuild(): boolean {
return typeof (globalThis as any).caches !== 'undefined' && typeof (process as any)?.cwd !== 'function';
}
if (isWorkerBuild() && usesDirectoryImport) { /* use pre-built routes */ } Type guard
const isWorkerContext = (): boolean => typeof (globalThis as any).WRAPERS || (typeof window === 'undefined' && typeof (globalThis as any).caches !== 'undefined' && !(globalThis as any).process?.versions?.node);
Try / catch
try {
return directoryImport(opts);
} catch (e) {
if (/not available in Worker builds/.test((e as Error).message)) {
return import('./generated/routes-worker.js');
}
throw e;
} Prevention
- Never import the real directory-import module from Worker-bundled code - keep it in the Node entry only.
- Generate a static route list at build time and consume it from the Worker bundle.
- Add a bundler externals rule so directory-import is excluded from the Worker graph.
When it happens
Trigger: Code that calls directoryImport({...}) is reachable in the Worker bundle - typically because a dynamic route loader or dev-only utility was not excluded from the Worker build graph.
Common situations: A new route or utility that auto-discovers files via directoryImport is imported (directly or transitively) by Worker-bundled code; a refactor that moves dev-mode code into a shared module pulled into the Worker entry.
Related errors
- exec is not supported in Cloudflare Workers
- spawn is not supported in Cloudflare Workers
- vm.Script.runInContext is not supported in Workers
- vm.Script.runInNewContext is not supported in Workers
- vm.Script.runInThisContext is not supported in Workers
AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12).
Data as JSON: /api/errors/a23f4c9c807dea7d.
Report an issue: GitHub.