{"record":{"id":"2bcad48cadcd0803","repo":"microsoft/TypeScript","slug":"getdefaultlibfilepath-is-only-supported-when-consu","errorCode":null,"errorMessage":"getDefaultLibFilePath is only supported when consumed as a node module. ","messagePattern":"getDefaultLibFilePath is only supported when consumed as a node module\\. ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/services/services.ts","lineNumber":3619,"sourceCode":"\r\nfunction isArgumentOfElementAccessExpression(node: Node) {\r\n    return node &&\r\n        node.parent &&\r\n        node.parent.kind === SyntaxKind.ElementAccessExpression &&\r\n        (node.parent as ElementAccessExpression).argumentExpression === node;\r\n}\r\n\r\n/**\r\n * Get the path of the default library files (lib.d.ts) as distributed with the typescript\r\n * node package.\r\n * The functionality is not supported if the ts module is consumed outside of a node module.\r\n */\r\nexport function getDefaultLibFilePath(options: CompilerOptions): string {\r\n    if (sys) {\r\n        return combinePaths(getDirectoryPath(normalizePath(sys.getExecutingFilePath())), getDefaultLibFileName(options));\r\n    }\r\n\r\n    throw new Error(\"getDefaultLibFilePath is only supported when consumed as a node module. \");\r\n}\r\n\r\nsetObjectAllocator(getServicesObjectAllocator());\r\n","sourceCodeStart":3601,"sourceCodeEnd":3623,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/services/services.ts#L3601-L3623","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","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.","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.","Guard the call: check `if (ts.sys)` before invoking getDefaultLibFilePath and fall back to getDefaultLibFileName otherwise."],"exampleFix":"// before (throws in browser / non-Node runtime)\nconst libPath = ts.getDefaultLibFilePath(options);\n\n// after (path-free; works everywhere)\nconst libFileName = ts.getDefaultLibFileName(options);\n// provide lib source through your host instead of the filesystem","handlingStrategy":"validation","validationCode":"// Probe the environment before calling the path-based API.\nfunction resolveLibFile(options: ts.CompilerOptions): string {\n  if (ts.sys) {\n    return ts.getDefaultLibFilePath(options);\n  }\n  // Non-Node runtime: use the path-free filename and supply contents yourself.\n  return ts.getDefaultLibFileName(options);\n}","typeGuard":"function supportsDefaultLibFilePath(): boolean {\n  return typeof ts !== \"undefined\" && !!ts.sys && typeof ts.sys.getExecutingFilePath === \"function\";\n}","tryCatchPattern":"try {\n  libPath = ts.getDefaultLibFilePath(options);\n} catch (e) {\n  if (e instanceof Error && /getDefaultLibFilePath is only supported when consumed as a node module/.test(e.message)) {\n    libFileName = ts.getDefaultLibFileName(options); // resolve contents via host\n  } else throw e;\n}","preventionTips":["In browser/bundler builds, reach for ts.getDefaultLibFileName and a custom host instead of getDefaultLibFilePath.","Confirm ts.sys is initialized (Node entrypoint) before relying on any path-based compiler API.","When implementing a LanguageServiceHost, override getDefaultLibFileName so the service never needs the filesystem path."],"tags":["compiler-host","sys","browser","node","lib-d-ts"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}