microsoft/TypeScript · error · Error

getDefaultLibFilePath is only supported when consumed as a n

Error message

getDefaultLibFilePath is only supported when consumed as a node module. 

What it means

Thrown by ts.getDefaultLibFilePath when the global ts.sys host is unavailable. The function resolves the lib directory by calling sys.getExecutingFilePath() and combining it with getDefaultLibFileName(options); if sys is falsy it throws because there is no filesystem path to locate the shipped lib.d.ts files. ts.sys is populated automatically when TypeScript is loaded as a Node module, but in browser bundles, custom runtimes, or environments where the System was never installed, sys stays undefined and the path-based API cannot work. Callers in such environments should use the path-free getDefaultLibFileName(options) instead and supply lib file contents through their own host.

Source

Thrown at src/services/services.ts:3619

function isArgumentOfElementAccessExpression(node: Node) {
    return node &&
        node.parent &&
        node.parent.kind === SyntaxKind.ElementAccessExpression &&
        (node.parent as ElementAccessExpression).argumentExpression === node;
}

/**
 * Get the path of the default library files (lib.d.ts) as distributed with the typescript
 * node package.
 * The functionality is not supported if the ts module is consumed outside of a node module.
 */
export function getDefaultLibFilePath(options: CompilerOptions): string {
    if (sys) {
        return combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibFileName(options));
    }

    throw new Error("getDefaultLibFilePath is only supported when consumed as a node module. ");
}

setObjectAllocator(getServicesObjectAllocator());

View on GitHub (pinned to b465fdbfe1)

Solutions

  1. In non-Node environments, do not call getDefaultLibFilePath; call ts.getDefaultLibFileName(options) (which only returns a filename like lib.es2020.d.ts) and resolve/provide the lib file yourself via your host.
  2. If you actually have a filesystem, ensure ts.sys is installed — use the Node entrypoint (`require("typescript")` from Node) rather than a browser/edge bundle, or set ts.sys to a custom System implementation before calling.
  3. For a browser LanguageService, implement getDefaultLibFileName on your LanguageServiceHost to return the lib filename and feed lib source through readFile/fileExists from a bundled copy.
  4. Guard the call: check `if (ts.sys)` before invoking getDefaultLibFilePath and fall back to getDefaultLibFileName otherwise.

Example fix

// before (throws in browser / non-Node runtime)
const libPath = ts.getDefaultLibFilePath(options);

// after (path-free; works everywhere)
const libFileName = ts.getDefaultLibFileName(options);
// provide lib source through your host instead of the filesystem
Defensive patterns

Strategy: validation

Validate before calling

// Probe the environment before calling the path-based API.
function resolveLibFile(options: ts.CompilerOptions): string {
  if (ts.sys) {
    return ts.getDefaultLibFilePath(options);
  }
  // Non-Node runtime: use the path-free filename and supply contents yourself.
  return ts.getDefaultLibFileName(options);
}

Type guard

function supportsDefaultLibFilePath(): boolean {
  return typeof ts !== "undefined" && !!ts.sys && typeof ts.sys.getExecutingFilePath === "function";
}

Try / catch

try {
  libPath = ts.getDefaultLibFilePath(options);
} catch (e) {
  if (e instanceof Error && /getDefaultLibFilePath is only supported when consumed as a node module/.test(e.message)) {
    libFileName = ts.getDefaultLibFileName(options); // resolve contents via host
  } else throw e;
}

Prevention

When it happens

Trigger: Calling ts.getDefaultLibFilePath(options) in a non-Node context: a browser bundle of TypeScript (webpack/vite), a worker, a custom System-backed host, or any runtime where ts.sys has not been initialized. Also reproducible in tests that import the compiler without the Node system shim. The branch taken is the `!sys` path inside getDefaultLibFilePath.

Common situations: Bundling the TypeScript compiler for the browser and calling getDefaultLibFilePath to compute lib paths. Using a stripped-down compiler build that omits the Node sys initialization. Porting a Node-based compiler invocation to an isomorphic/browser environment without switching to getDefaultLibFileName and a custom LanguageServiceHost that returns lib contents via fileExists/readFile/getDefaultLibFileName.

Related errors


AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12). Data as JSON: /api/errors/2bcad48cadcd0803. Report an issue: GitHub.