{"record":{"id":"603f3ee1d02c50e6","repo":"denoland/deno","slug":"err-invalid-arg-type-603f3e","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"directory\" argument must be of type string. Received ${directory}","messagePattern":"The \"directory\" argument must be of type string\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_process/process.ts","lineNumber":60,"sourceCode":"  if (build.arch == \"x86_64\") {\n    return \"x64\";\n  } else if (build.arch == \"aarch64\") {\n    return \"arm64\";\n  } else if (build.arch == \"riscv64gc\") {\n    return \"riscv64\";\n  } else if (build.arch == \"loongarch64\") {\n    return \"loong64\";\n  } else if (build.arch == \"powerpc64le\") {\n    return \"ppc64\";\n  } else {\n    throw new Error(\"unreachable\");\n  }\n}\n\n/** https://nodejs.org/api/process.html#process_process_chdir_directory */\nfunction chdir(directory: string): void {\n  if (typeof directory !== \"string\") {\n    throw new ERR_INVALID_ARG_TYPE(\"directory\", \"string\", directory);\n  }\n  // Node's chdir error carries `path` (the cwd before chdir), `dest` (the\n  // target), and `syscall: 'chdir'`. Snapshot the cwd before attempting the\n  // change so the error's `path` matches Node's behaviour. If the current\n  // cwd has been deleted (common in tmpdir cleanup during process exit),\n  // `fs.cwd()` itself throws -- fall back to an empty string so the wrapper\n  // still has a sensible `path`, and don't surface the cwd lookup error.\n  let fromPath = \"\";\n  try {\n    fromPath = fs.cwd();\n  } catch {\n    // Ignore -- chdir() below will surface a chdir-shaped error.\n  }\n  try {\n    fs.chdir(directory);\n  } catch (err) {\n    throw denoErrorToNodeError(err as Error, {\n      syscall: \"chdir\",","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_process/process.ts#L42-L78","documentation":"process.chdir(directory) validates its argument up front: anything that is not a string (null, undefined, number, object, URL) throws ERR_INVALID_ARG_TYPE('directory', 'string', directory). The check runs before any syscall, so the current working directory is unchanged when it fires.","triggerScenarios":"process.chdir(null), process.chdir(123), process.chdir(new URL('file:///x')) (a URL object, not a string), or process.chdir(config.workdir) where config.workdir is undefined.","commonSituations":"Optional config values that are undefined by default; passing a URL or path object instead of a string; numbers leaking from parsed CLI input; a refactor that renames the variable leaving it undefined.","solutions":["Pass a plain string path","Default the value: process.chdir(dir ?? process.cwd())","Validate with typeof dir === 'string' and non-empty at the config boundary","Convert deliberately with String(dir) after an undefined check"],"exampleFix":"// before\nprocess.chdir(config.workdir); // workdir is undefined\n\n// after\nif (typeof config.workdir !== 'string' || config.workdir === '') {\n  throw new Error('config.workdir must be a directory path string');\n}\nprocess.chdir(config.workdir);","handlingStrategy":"type-guard","validationCode":"if (typeof directory !== 'string' || directory.length === 0) {\n  throw new TypeError('directory must be a non-empty string');\n}\nprocess.chdir(directory);","typeGuard":"const isDirectoryString = (v) => typeof v === 'string' && v.length > 0;","tryCatchPattern":"try {\n  process.chdir(dir);\n} catch (e) {\n  if (e.code === 'ERR_INVALID_ARG_TYPE') {\n    // argument was not a string: fix the caller's value\n  } else if (e.code === 'ENOENT' || e.code === 'ENOTDIR') {\n    // directory missing or not a directory\n  } else throw e;\n}","preventionTips":["Resolve config paths early with a required-string helper","Remember chdir accepts only strings - no URL or path objects","Prefer absolute paths so cwd changes cannot break later relative lookups"],"tags":["process","filesystem","validation","node-compat"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}