DIYgod/RSSHub · error
vm.compileFunction is not supported in Workers
Error message
vm.compileFunction is not supported in Workers
What it means
vmShim.compileFunction() in lib/shims/node-module.ts throws. Workers cannot compile a string into a callable Function at runtime via the vm API (no V8 compileFunction exposed), so the call is hard-fail.
Source
Thrown at lib/shims/node-module.ts:76
throw new Error('vm.Script.runInThisContext is not supported in Workers');
}
}
const vmShim = {
createContext: (sandbox?: object) => sandbox || {},
runInContext: () => {
throw new Error('vm.runInContext is not supported in Workers');
},
runInNewContext: () => {
throw new Error('vm.runInNewContext is not supported in Workers');
},
runInThisContext: () => {
throw new Error('vm.runInThisContext is not supported in Workers');
},
Script: ScriptShim,
isContext: () => false,
compileFunction: () => {
throw new Error('vm.compileFunction is not supported in Workers');
},
};
// Child process shim (inline to avoid import cycle)
const child_process = {
execSync: (_command: string): Buffer => Buffer.from(''),
exec: () => {
throw new Error('exec is not supported in Cloudflare Workers');
},
spawn: () => {
throw new Error('spawn is not supported in Cloudflare Workers');
},
fork: () => {
throw new Error('fork is not supported in Cloudflare Workers');
},
execFile: () => {
throw new Error('execFile is not supported in Cloudflare Workers');
},View on GitHub (pinned to bed535e087)
Solutions
- Replace vm.compileFunction with a build-time compiled function or new Function (only if Workers' CSP allows).
- Static-import the compiled result instead.
- Keep the code path Node-only.
Example fix
// before const fn = vm.compileFunction(src, ['x']); // after import compiledFn from './compiled'; // produced at build time
Defensive patterns
Strategy: validation
Validate before calling
const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
// skip vm.compileFunction; use a build-time compiled function
} Type guard
function isVmShimError(e: unknown): e is Error {
return e instanceof Error && /not supported in Workers/.test(e.message);
} Try / catch
try {
if (vmUsable) return vm.compileFunction(src, ['x']);
else return precompiled;
} catch (e) {
if (isVmShimError(e)) return precompiled;
throw e;
} Prevention
- Do not build functions from source at runtime under Workers.
- Compile DSLs/templates at build time instead.
- Keep vm-based compilers on the Node target.
When it happens
Trigger: Calling vm.compileFunction(source, params) under Workers — used by some module systems and meta-templating libraries to build functions from source.
Common situations: Custom module loaders; DSL/template compilers built on vm.compileFunction.
Related errors
- vm.Script.runInContext is not supported in Workers
- vm.Script.runInNewContext is not supported in Workers
- vm.Script.runInThisContext is not supported in Workers
- vm.runInContext is not supported in Workers
- vm.runInNewContext is not supported in Workers
AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12).
Data as JSON: /api/errors/ace95533b963104c.
Report an issue: GitHub.