resolvesTo

tcmts.loader.packages.resolvesTo · #pdfuvmpq · term · runs on the server

uses node:path

Where Node (and Vite, and TypeScript) would find package name from inside directory dir.

import fs from "node:fs";
import path from "node:path";

export function resolvesTo(name: string, dir: string): string | undefined {
  for (let d = dir; ; d = path.dirname(d)) {
    const candidate = path.join(d, "node_modules", name);
    if (fs.existsSync(path.join(candidate, "package.json"))) return fs.realpathSync(candidate);
    if (path.dirname(d) === d) return undefined;
  }
}

used by