# node:path

The `node:path` module provides the Node.js utilities for working with file and directory paths. Its functions join, split, normalize, and parse paths, and a function uses them to build paths when it routes requests or composes asset references. Azion Runtime does not give a function access to a real filesystem, so every `path` function operates on path strings only. For example, `path.resolve()` returns a string without reading a directory.

---

## Examples

The functions below run on their own: each one imports `path` from `node:path`, and each output is the one a deployed function returns.

### Basic path operations

This function joins three segments into one path, logs it, and returns it in the response body:

```javascript
/**
 * An example of using the Node.js Path API in an Azion Function.
 * Support:
 * - Partially supported (Extended by library `path-browserify`)
 * @module runtime-apis/nodejs/path/main
 * @example
 * // Build and run with the Azion CLI:
 * azion build
 * azion dev
 */
import path from "node:path";

/**
 * An example of using the Node.js Path API in an Azion Function.
 * @param {*} event
 * @returns {Promise<Response>}
 */
const main = async (event) => {
  const pathName = path.join("/", "images", "image.jpg");
  console.log("Path", pathName);
  return new Response(`Path: ${pathName}`);
};

export default main;
```

The function logs `Path /images/image.jpg` and responds with this body:

```text
Path: /images/image.jpg
```

### URL routing with path

This function splits the request path into its directory, file name, and extension, then builds an API path and an absolute path from the parts:

```javascript
import path from "node:path";

const main = async (event) => {
  const requestUrl = new URL(event.request.url);
  const pathname = requestUrl.pathname;

  // Extract path components
  const dirname = path.dirname(pathname);
  const basename = path.basename(pathname);
  const extname = path.extname(pathname);

  console.log("Directory:", dirname);
  console.log("Filename:", basename);
  console.log("Extension:", extname);

  // Build a new path for routing
  const apiPath = path.join("/api", "v1", "users", basename);
  console.log("API path:", apiPath);

  // In Azion Runtime, path.resolve() manipulates path strings only;
  // it does not read a filesystem
  const absolutePath = path.resolve("/app", "public", "images", "logo.png");
  console.log("Absolute path:", absolutePath);

  return new Response(JSON.stringify({
    original: pathname,
    dirname,
    basename,
    extname,
    apiPath,
    absolutePath
  }), {
    headers: { "Content-Type": "application/json" }
  });
};

export default main;
```

For a request to `/docs/getting-started`, a path with no file extension, the function responds with each part of the path and the two paths it built:

```json
{"original":"/docs/getting-started","dirname":"/docs","basename":"getting-started","extname":"","apiPath":"/api/v1/users/getting-started","absolutePath":"/app/public/images/logo.png"}
```

### Path normalization and parsing

This function normalizes a path that holds `..`, `.`, and a double slash. It then parses a path into its parts, builds a path from an object, and checks whether two paths are absolute:

```javascript
import path from "node:path";

const main = async (event) => {
  // Normalize messy paths
  const messyPath = "/users/../public/./images//photo.jpg";
  const normalizedPath = path.normalize(messyPath);
  console.log("Normalized:", normalizedPath);
  // /public/images/photo.jpg

  // Parse path into components
  const filePath = "/assets/css/styles.css";
  const parsed = path.parse(filePath);
  console.log("Parsed:", parsed);
  // { root: '/', dir: '/assets/css', base: 'styles.css', ext: '.css', name: 'styles' }

  // Format path from object
  const formatted = path.format({
    root: "/",
    dir: "/data/documents",
    base: "report.pdf"
  });
  console.log("Formatted:", formatted);

  // Check if path is absolute
  const isAbs1 = path.isAbsolute("/assets/app.js");
  const isAbs2 = path.isAbsolute("../relative/path");
  console.log("Is absolute:", isAbs1, isAbs2);

  return new Response(JSON.stringify({
    normalizedPath,
    parsed,
    formatted,
    isAbsolute: { absolute: isAbs1, relative: isAbs2 }
  }), {
    headers: { "Content-Type": "application/json" }
  });
};

export default main;
```

The function responds with the normalized path, the parsed parts, the formatted path, and the two checks:

```json
{"normalizedPath":"/public/images/photo.jpg","parsed":{"root":"/","dir":"/assets/css","base":"styles.css","ext":".css","name":"styles"},"formatted":"/data/documents/report.pdf","isAbsolute":{"absolute":true,"relative":false}}
```

### Path separators and platform variants

This function joins the same segments with the default, POSIX, and Windows variants of the module, and reads each separator and the delimiter:

```javascript
import path from "node:path";

const main = async (event) => {
  // Default path methods (POSIX rules in Azion Runtime)
  const defaultJoin = path.join("folder", "subfolder", "file.txt");
  const defaultSep = path.sep;

  // POSIX-style paths (forward slashes)
  // path.posix and path.win32 are properties of the main module
  const posixJoin = path.posix.join("folder", "subfolder", "file.txt");
  const posixSep = path.posix.sep;

  // Windows-style methods: in Azion Runtime, they also use forward slashes
  const win32Join = path.win32.join("folder", "subfolder", "file.txt");
  const win32Sep = path.win32.sep;

  console.log("Default join:", defaultJoin);
  console.log("POSIX join:", posixJoin);
  console.log("Windows join:", win32Join);

  // Get delimiter for PATH environment variable
  const delimiter = path.delimiter;
  console.log("Path delimiter:", delimiter);

  return new Response(JSON.stringify({
    default: { join: defaultJoin, sep: defaultSep },
    posix: { join: posixJoin, sep: posixSep },
    win32: { join: win32Join, sep: win32Sep },
    delimiter
  }), {
    headers: { "Content-Type": "application/json" }
  });
};

export default main;
```

The function responds with the same joined path and the `/` separator for all three variants, and `:` as the delimiter:

```json
{"default":{"join":"folder/subfolder/file.txt","sep":"/"},"posix":{"join":"folder/subfolder/file.txt","sep":"/"},"win32":{"join":"folder/subfolder/file.txt","sep":"/"},"delimiter":":"}
```

---

## Supported APIs

The table lists the status of each `node:path` API in Azion Runtime:

| API                 | Status                 |
| ------------------- | ---------------------- |
| `path.basename()`   | 🟢 Supported           |
| `path.dirname()`    | 🟢 Supported           |
| `path.extname()`    | 🟢 Supported           |
| `path.format()`     | 🟢 Supported           |
| `path.isAbsolute()` | 🟢 Supported           |
| `path.join()`       | 🟢 Supported           |
| `path.normalize()`  | 🟢 Supported           |
| `path.parse()`      | 🟢 Supported           |
| `path.relative()`   | 🟢 Supported           |
| `path.resolve()`    | 🟢 Supported           |
| `path.sep`          | 🟢 Supported           |
| `path.delimiter`    | 🟢 Supported           |
| `path.posix`        | 🟢 Supported           |
| `path.win32`        | 🟡 Partially supported |

`path.win32.join()` joins segments with `/`, so `path.win32.join("C:\\a", "b")` returns `C:/a/b`, and `path.win32.sep` is `/`.

Every `path` function operates on path strings, without filesystem access. `path.resolve()` resolves relative segments against `/`, so `path.resolve("a", "b")` returns `/a/b`. To work with the path of a request, read it with the Web `URL` API, as in `new URL(event.request.url).pathname`, and pass it to the `node:path` functions.

---

## Related resources

- [Node.js APIs](/en/documentation/devtools/runtime/node.md): The status of every Node.js module in Azion Runtime, `path` included.
- [Use Node.js APIs through polyfills](/en/documentation/guides/application-development/functions-and-runtime/use-polyfills.md): How the build turns `node:` imports into code that runs in Azion Runtime.
- [node:url](/en/documentation/devtools/runtime/node/url.md): The Node.js module that parses and formats the URLs whose paths `node:path` handles.
- [Node.js path documentation](https://nodejs.org/api/path.html): The full Node.js reference for every `node:path` API in the table.
