Cross-platform path manipulation utilities ensuring consistent path handling across Windows, macOS, and Linux systems. These functions handle platform-specific path separators and edge cases.
Joins path segments with proper handling of Windows path separators.
/**
* Joins path segments and handles Windows path separators by escaping backslashes
* @param paths - Path segments to join
* @returns Joined path string with properly escaped separators
*/
function joinPath(...paths: Array<string>): string;Usage Examples:
import { joinPath } from "gatsby-core-utils";
// Cross-platform path joining
const configPath = joinPath(process.cwd(), "src", "gatsby-config.js");
console.log(configPath);
// Unix: "/home/user/project/src/gatsby-config.js"
// Windows: "C:\\\\Users\\\\user\\\\project\\\\src\\\\gatsby-config.js"
// Build system paths
const publicPath = joinPath(siteRoot, "public", "static", "images");
const componentPath = joinPath("src", "components", "Layout.tsx");Converts Windows backslash paths to forward slash paths for URLs and cross-platform consistency.
/**
* Converts Windows backslash paths to forward slash paths
* Handles extended-length paths (\\\\?\\ prefix) by leaving them unchanged
* @param path - Path to convert
* @returns Path with forward slashes, preserving extended-length paths
*/
function slash(path: string): string;Usage Examples:
import { slash } from "gatsby-core-utils";
// Normalize Windows paths for URLs
const windowsPath = "C:\\Users\\user\\project\\src\\pages\\index.js";
const normalizedPath = slash(windowsPath);
console.log(normalizedPath); // "C:/Users/user/project/src/pages/index.js"
// Safe for extended-length paths
const extendedPath = "\\\\?\\C:\\VeryLongPath\\...";
const safeNormalized = slash(extendedPath);
console.log(safeNormalized); // "\\\\?\\C:\\VeryLongPath\\..." (unchanged)Joins path segments with forward slashes specifically for URL construction.
/**
* Joins path segments with forward slashes, suitable for URLs
* Always uses forward slashes regardless of platform
* @param segments - Path segments to join
* @returns URL-safe path with forward slashes
*/
function urlResolve(...segments: Array<string>): string;Usage Examples:
import { urlResolve } from "gatsby-core-utils";
// Build URLs for static assets
const assetUrl = urlResolve("/static", "images", "hero.jpg");
console.log(assetUrl); // "/static/images/hero.jpg"
// API endpoint construction
const apiPath = urlResolve("/api", "v1", "users", userId);
const fullUrl = `https://api.example.com${apiPath}`;
// Relative URL building
const relativePath = urlResolve(".", "assets", "css", "main.css");
console.log(relativePath); // "./assets/css/main.css"Checks if a filename corresponds to Node.js internal modules for filtering and special handling.
/**
* Checks if a filename matches Node.js internal module paths
* Includes core modules and internal/ directory modules
* @param fileName - Filename to check (just the filename, not full path)
* @returns True if the filename is a Node.js internal module
*/
function isNodeInternalModulePath(fileName: string): boolean;Usage Examples:
import { isNodeInternalModulePath } from "gatsby-core-utils";
// Filter out Node.js internals from stack traces
const stackLines = error.stack.split('\n');
const userStackLines = stackLines.filter(line => {
const filename = extractFilename(line);
return !isNodeInternalModulePath(filename);
});
// Skip processing internal modules in build tools
if (isNodeInternalModulePath("fs.js")) {
console.log("Skipping Node.js internal module");
}
// Examples of detected internal modules
console.log(isNodeInternalModulePath("http.js")); // true
console.log(isNodeInternalModulePath("internal/util.js")); // true
console.log(isNodeInternalModulePath("my-module.js")); // falsejoinPath() doubles backslashes (\\) to ensure proper escapingslash() converts backslashes to forward slashes except for extended-length paths\\\\?\\) are preserved unchangedjoinPath() behaves like standard path.join()slash() has no effect since paths already use forward slashesurlResolve() works identically across all platformsimport { joinPath, slash, urlResolve } from "gatsby-core-utils";
// These produce consistent results across platforms:
const buildPath = joinPath("build", "static", "js");
const urlPath = urlResolve("/", "static", "js");
const normalizedPath = slash(someWindowsPath);
// Platform-aware file operations
const configFile = joinPath(process.cwd(), "gatsby-config.js");
const publicUrl = urlResolve(pathPrefix, "page-data", pagePath);All path utility functions are designed to handle edge cases gracefully:
These utilities are extensively used throughout Gatsby's build process: