swagger-api/swagger-ui · error · Error

getAbsoluteFSPath can only be called within a Nodejs environ

Error message

getAbsoluteFSPath can only be called within a Nodejs environment

What it means

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.

Source

Thrown at swagger-ui-dist-package/absolute-path.js:11

/*
 * getAbsoluteFSPath
 * @return {string} When run in NodeJS env, returns the absolute path to the current directory
 *                  When run outside of NodeJS, will return an error message
 */
const getAbsoluteFSPath = function () {
  // detect whether we are running in a browser or nodejs
  if (typeof module !== "undefined" && module.exports) {
    return require("path").resolve(__dirname)
  }
  throw new Error('getAbsoluteFSPath can only be called within a Nodejs environment');
}

module.exports = getAbsoluteFSPath

View on GitHub (pinned to 3d9d0916d4)

Solutions

  1. In browser code, import only the named browser export (import { SwaggerUIBundle } from "swagger-ui-dist") and never call getAbsoluteFSPath client-side.
  2. 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.
  3. 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.
  4. 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.

Example fix

// before (runs in the browser bundle)
import { SwaggerUIBundle, getAbsoluteFSPath } from "swagger-ui-dist"
const assetPath = getAbsoluteFSPath() // throws

// after (split by environment)
// server.js (Node only)
const { getAbsoluteFSPath } = require("swagger-ui-dist")
app.use(express.static(getAbsoluteFSPath()))
// client.js (browser)
import { SwaggerUIBundle } from "swagger-ui-dist"
Defensive patterns

Strategy: validation

Validate before calling

// Only call getAbsoluteFSPath in a Node/CommonJS context.
const isNode =
  typeof module !== "undefined" && !!module.exports &&
  typeof process !== "undefined" && !!process.versions && !!process.versions.node

let assetPath = null
if (isNode) {
  assetPath = require("swagger-ui-dist").getAbsoluteFSPath()
}

Type guard

// True only when getAbsoluteFSPath can run safely.
const supportsGetAbsoluteFSPath = () =>
  typeof module !== "undefined" && !!module.exports &&
  typeof process !== "undefined" && !!process.versions && !!process.versions.node

Try / catch

let assetPath
try {
  assetPath = require("swagger-ui-dist").getAbsoluteFSPath()
} catch (e) {
  if (/getAbsoluteFSPath can only be called within a Nodejs environment/.test(e.message)) {
    // Browser bundle: skip filesystem path resolution.
    assetPath = null
  } else {
    throw e
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.


AI-assisted analysis of swagger-api/swagger-ui@3d9d0916d4 (2026-08-13). Data as JSON: /api/errors/e845fc246577afca. Report an issue: GitHub.