denoland/deno · error · TypeError

Resolved a relative path without a CWD.

Error message

Resolved a relative path without a CWD.

What it means

After `path.win32.resolve` finds a drive letter but no absolute target, it needs the drive-specific working directory: the `=C:` environment variable or `Deno.cwd`. When neither `Deno.env.get` nor `Deno.cwd` is a function, it throws this TypeError.

Source

Thrown at ext/node/polyfills/path/_win32.ts:119

      path = pathSegments[i];
    } else if (!resolvedDevice) {
      if (typeof Deno?.cwd !== "function") {
        throw new TypeError("Resolved a drive-letter-less path without a CWD.");
      }
      path = Deno.cwd();
      if (
        pathSegments.length === 0 ||
        ((pathSegments.length === 1 &&
          (pathSegments[0] === "" || pathSegments[0] === ".")) &&
          isPathSeparator(StringPrototypeCharCodeAt(path, 0)))
      ) {
        return path;
      }
    } else {
      if (
        typeof Deno?.env?.get !== "function" || typeof Deno?.cwd !== "function"
      ) {
        throw new TypeError("Resolved a relative path without a CWD.");
      }
      // Windows has the concept of drive-specific current working
      // directories. If we've resolved a drive letter but not yet an
      // absolute path, get cwd for that drive, or the process cwd if
      // the drive cwd is not available. We're sure the device is not
      // a UNC path at this points, because UNC paths are always absolute.
      path = globalThis.process.env[`=${resolvedDevice}`] || Deno.cwd();

      // Verify that a cwd was found and that it actually points
      // to our drive. If not, default to the drive's root.
      if (
        path === undefined ||
        (StringPrototypeToLowerCase(StringPrototypeSlice(path, 0, 2)) !==
            StringPrototypeToLowerCase(resolvedDevice) &&
          StringPrototypeCharCodeAt(path, 2) === CHAR_BACKWARD_SLASH)
      ) {
        path = `${resolvedDevice}\\`;
      }

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Use fully absolute targets: `path.win32.resolve('C:\\base', 'file.txt')`.
  2. Expand drive-relative inputs against a known base before resolving.
  3. Ensure the environment exposes env and cwd APIs — the check tests their presence as functions.
  4. Prefer native `path.resolve` unless Windows drive semantics are required.

Example fix

// before
const p = path.win32.resolve('C:', 'file.txt'); // drive-relative, needs cwd

// after
const p = path.win32.resolve('C:\\base', 'file.txt');
Defensive patterns

Strategy: validation

Validate before calling

const hasRuntimeEnv = () =>
  typeof (globalThis as any)?.Deno?.env?.get === 'function' &&
  typeof (globalThis as any)?.Deno?.cwd === 'function';
const isAbsoluteWin = (p) => /^[A-Za-z]:[\\/]/.test(p);
if (!hasRuntimeEnv() && args.some((a) => /^[A-Za-z]:[^\\/]/.test(String(a)))) {
  throw new Error('drive-relative input needs env/cwd; pass an absolute base');
}
return path.win32.resolve(...args);

Try / catch

try {
  p = path.win32.resolve(...args);
} catch (err) {
  if (err instanceof TypeError && /without a CWD/.test(err.message)) {
    throw new Error('expand drive-relative paths against an absolute base', { cause: err });
  }
  throw err;
}

Prevention

When it happens

Trigger: `path.win32.resolve('C:', 'file.txt')` or `path.win32.resolve('C:file.txt')` — drive-relative inputs — in a runtime without the Deno global (bundled polyfill, non-Deno embedder, stripped sandbox).

Common situations: Same contexts as the drive-letter-less case: win32 path logic unit-tested outside Deno, polyfills bundled for other targets. Drive-relative Windows paths carried in config ('D:data') resolved on a machine without Windows.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/53c5baae6f8dfce2. Report an issue: GitHub.