{"record":{"id":"e845fc246577afca","repo":"swagger-api/swagger-ui","slug":"getabsolutefspath-can-only-be-called-within-a-node","errorCode":null,"errorMessage":"getAbsoluteFSPath can only be called within a Nodejs environment","messagePattern":"getAbsoluteFSPath can only be called within a Nodejs environment","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"swagger-ui-dist-package/absolute-path.js","lineNumber":11,"sourceCode":"/*\n * getAbsoluteFSPath\n * @return {string} When run in NodeJS env, returns the absolute path to the current directory\n *                  When run outside of NodeJS, will return an error message\n */\nconst getAbsoluteFSPath = function () {\n  // detect whether we are running in a browser or nodejs\n  if (typeof module !== \"undefined\" && module.exports) {\n    return require(\"path\").resolve(__dirname)\n  }\n  throw new Error('getAbsoluteFSPath can only be called within a Nodejs environment');\n}\n\nmodule.exports = getAbsoluteFSPath\n","sourceCodeStart":1,"sourceCodeEnd":15,"githubUrl":"https://github.com/swagger-api/swagger-ui/blob/3d9d0916d4c0542c50639a9a0e680ab1475683a6/swagger-ui-dist-package/absolute-path.js#L1-L15","documentation":"getAbsoluteFSPath is exported by the swagger-ui-dist package (swagger-ui-dist-package/absolute-path.js) to return the absolute filesystem path of the dist folder, for use when statically serving Swagger UI's assets from a Node server. It works only when the CommonJS module system is present (typeof module !== \"undefined\" && module.exports); if that shim is absent — i.e. the code is running in a browser/ESM bundle — it throws this Error. It is a Node-only utility and is not part of the browser API.","triggerScenarios":"Calling require(\"swagger-ui-dist\").getAbsoluteFSPath() inside a bundle whose browser target lacks a CommonJS shim (Webpack/Vite/Rollup browser build with CJS interop stripped); importing the dist package's index.js in client code, which re-exports getAbsoluteFSPath; running under a pure ESM loader that does not define module.exports; SSR frameworks that execute one module graph in both Node and browser contexts.","commonSituations":"Accidentally importing the whole 'swagger-ui-dist' entry point in browser code instead of importing only SwaggerUIBundle; bundlers that pull absolute-path.js into the browser graph because something references the package root; migrating from CommonJS to Vite/esbuild where module.exports is polyfilled inconsistently or not at all.","solutions":["In browser code, import only the named browser export (import { SwaggerUIBundle } from \"swagger-ui-dist\") and never call getAbsoluteFSPath client-side.","Call getAbsoluteFSPath only in a Node/server entry (Express, http, etc.) that serves the assets, and ensure that file is excluded from the browser bundle.","With Vite/Rollup, mark 'swagger-ui-dist' external for the server build or isolate the call behind a dynamic import in a Node-only path.","If you only need asset URLs in the browser, reference the published files directly (e.g. /swagger-ui.css, /swagger-ui-bundle.js) instead of resolving them from the filesystem."],"exampleFix":"// before (runs in the browser bundle)\nimport { SwaggerUIBundle, getAbsoluteFSPath } from \"swagger-ui-dist\"\nconst assetPath = getAbsoluteFSPath() // throws\n\n// after (split by environment)\n// server.js (Node only)\nconst { getAbsoluteFSPath } = require(\"swagger-ui-dist\")\napp.use(express.static(getAbsoluteFSPath()))\n// client.js (browser)\nimport { SwaggerUIBundle } from \"swagger-ui-dist\"","handlingStrategy":"validation","validationCode":"// Only call getAbsoluteFSPath in a Node/CommonJS context.\nconst isNode =\n  typeof module !== \"undefined\" && !!module.exports &&\n  typeof process !== \"undefined\" && !!process.versions && !!process.versions.node\n\nlet assetPath = null\nif (isNode) {\n  assetPath = require(\"swagger-ui-dist\").getAbsoluteFSPath()\n}","typeGuard":"// True only when getAbsoluteFSPath can run safely.\nconst supportsGetAbsoluteFSPath = () =>\n  typeof module !== \"undefined\" && !!module.exports &&\n  typeof process !== \"undefined\" && !!process.versions && !!process.versions.node","tryCatchPattern":"let assetPath\ntry {\n  assetPath = require(\"swagger-ui-dist\").getAbsoluteFSPath()\n} catch (e) {\n  if (/getAbsoluteFSPath can only be called within a Nodejs environment/.test(e.message)) {\n    // Browser bundle: skip filesystem path resolution.\n    assetPath = null\n  } else {\n    throw e\n  }\n}","preventionTips":["Keep getAbsoluteFSPath calls in server-only entry files and exclude them from browser bundles.","In Webpack, split server/client configs or mark 'swagger-ui-dist' external for the server build.","In Vite, import getAbsoluteFSPath only in SSR/server modules so it never enters the client graph."],"tags":["environment","node","browser","bundler","commonjs","swagger-ui-dist"],"backgroundTag":null,"analyzedSha":"3d9d0916d4c0542c50639a9a0e680ab1475683a6","analyzedAt":"2026-08-13T10:03:12.308Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}