{"record":{"id":"2488ab93d498ada1","repo":"denoland/deno","slug":"err-invalid-arg-type-2488ab","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"${name}\" argument must be of type function. Received ${actual}","messagePattern":"The \"(.+?)\" argument must be of type function\\. Received (.+?)","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/vm.js","lineNumber":216,"sourceCode":"  createCachedData() {\n    return Buffer.from(op_vm_script_create_cached_data(this.#inner));\n  }\n}\n\nfunction finishDynamicImportResult(result) {\n  if (isModule(result)) {\n    return PromisePrototypeThen(result.evaluate(), () => result.namespace);\n  }\n  return result;\n}\n\nfunction validateImportModuleDynamically(value, name) {\n  if (\n    value !== undefined &&\n    value !== USE_MAIN_CONTEXT_DEFAULT_LOADER &&\n    typeof value !== \"function\"\n  ) {\n    throw new ERR_INVALID_ARG_TYPE(\n      name,\n      \"function\",\n      value,\n    );\n  }\n}\n\nfunction getImportModuleDynamicallyId(value, name, getReferrer) {\n  validateImportModuleDynamically(value, name);\n  if (value === USE_MAIN_CONTEXT_DEFAULT_LOADER) {\n    return -1;\n  }\n  if (value === undefined) {\n    return 0;\n  }\n  return op_vm_dynamic_import_callback_register(\n    (specifier, importAttributes) => {\n      return PromisePrototypeThen(","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/vm.js#L198-L234","documentation":"When compiling sandboxed code that uses dynamic import(), options.importModuleDynamically must be a callback, the sentinel vm.USE_MAIN_CONTEXT_DEFAULT_LOADER, or undefined. validateImportModuleDynamically rejects every other value with ERR_INVALID_ARG_TYPE because the loader has to be callable or that exact well-known constant.","triggerScenarios":"new vm.Script(code, { importModuleDynamically: 'main' }), vm.compileFunction(src, [], { importModuleDynamically: {} }), or wiring the option from config/env as a string or boolean.","commonSituations":"Copying the option into YAML/JSON config where it becomes a string; porting code that targeted an older string-based convention; passing a module namespace object or an import() promise instead of the function itself.","solutions":["Pass a function: importModuleDynamically: (specifier, referrer, attributes) => import(specifier).","Or pass the sentinel vm.USE_MAIN_CONTEXT_DEFAULT_LOADER for the default main-context loader.","Omit the option entirely when custom dynamic-import behavior in the sandbox is not needed."],"exampleFix":"// before\nnew vm.Script(code, { importModuleDynamically: \"main\" }); // ERR_INVALID_ARG_TYPE\n\n// after\nconst script = new vm.Script(code, {\n  importModuleDynamically: (specifier) => import(specifier),\n});","handlingStrategy":"validation","validationCode":"import vm from \"node:vm\";\n\nconst isValidImd = (v) =>\n  v === undefined ||\n  v === vm.USE_MAIN_CONTEXT_DEFAULT_LOADER ||\n  typeof v === \"function\";\n\nconst safeImd = isValidImd(opts.importModuleDynamically)\n  ? opts.importModuleDynamically\n  : undefined;","typeGuard":"const isValidImportModuleDynamically = (v) =>\n  v === undefined ||\n  v === vm.USE_MAIN_CONTEXT_DEFAULT_LOADER ||\n  typeof v === \"function\";","tryCatchPattern":null,"preventionTips":["Keep loader configuration in code, not in serialized config files.","The sentinel is a unique constant value — never its string name.","vm.compileFunction accepts the same option; validate once in a shared helper."],"tags":["vm","dynamic-import","err-invalid-arg-type","sandbox"],"backgroundTag":"callback-type-validation","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}