On this page
@std/path
Overview Jump to heading
Utilities for working with OS-specific file paths.
Functions from this module will automatically switch to support the path style
of the current OS, either windows for Microsoft Windows, or posix for
every other operating system, eg. Linux, MacOS, BSD etc.
To use functions for a specific path style regardless of the current OS import the modules from the platform sub directory instead.
Basic Path Operations
import * as path from "@std/path";
import { assertEquals } from "@std/assert";
// Get components of a path
if (Deno.build.os === "windows") {
assertEquals(path.basename("C:\\Users\\user\\file.txt"), "file.txt");
assertEquals(path.dirname("C:\\Users\\user\\file.txt"), "C:\\Users\\user");
assertEquals(path.extname("C:\\Users\\user\\file.txt"), ".txt");
} else {
assertEquals(path.basename("/home/user/file.txt"), "file.txt");
assertEquals(path.dirname("/home/user/file.txt"), "/home/user");
assertEquals(path.extname("/home/user/file.txt"), ".txt");
}
// Join path segments
if (Deno.build.os === "windows") {
assertEquals(path.join("C:\\", "Users", "docs", "file.txt"), "C:\\Users\\docs\\file.txt");
} else {
assertEquals(path.join("/home", "user", "docs", "file.txt"), "/home/user/docs/file.txt");
}
// Normalize a path
if (Deno.build.os === "windows") {
assertEquals(path.normalize("C:\\Users\\user\\..\\temp\\.\\file.txt"), "C:\\Users\\temp\\file.txt");
} else {
assertEquals(path.normalize("/home/user/../temp/./file.txt"), "/home/temp/file.txt");
}
// Resolve absolute path
if (Deno.build.os === "windows") {
const resolved = path.resolve("C:\\foo", "docs", "file.txt");
assertEquals(resolved, "C:\\foo\\docs\\file.txt");
assertEquals(path.isAbsolute(resolved), true);
} else {
const resolved = path.resolve("/foo", "docs", "file.txt");
assertEquals(resolved, "/foo/docs/file.txt");
assertEquals(path.isAbsolute(resolved), true);
}
// Get relative path
if (Deno.build.os === "windows") {
assertEquals(path.relative("C:\\Users", "C:\\Users\\docs\\file.txt"), "docs\\file.txt");
assertEquals(path.relative("C:\\Users", "D:\\Programs"), "D:\\Programs");
} else {
assertEquals(path.relative("/home/user", "/home/user/docs/file.txt"), "docs/file.txt");
assertEquals(path.relative("/home/user", "/var/data"), "../../var/data");
}
Path Parsing and Formatting
import * as path from "@std/path";
import { assertEquals } from "@std/assert";
if (Deno.build.os === "windows") {
const parsedWindows = path.parse("C:\\Users\\user\\file.txt");
assertEquals(parsedWindows.root, "C:\\");
assertEquals(parsedWindows.dir, "C:\\Users\\user");
assertEquals(parsedWindows.base, "file.txt");
assertEquals(parsedWindows.ext, ".txt");
assertEquals(parsedWindows.name, "file");
// Format path from components (Windows)
assertEquals(
path.format({ dir: "C:\\Users\\user", base: "file.txt" }),
"C:\\Users\\user\\file.txt"
);
} else {
const parsedPosix = path.parse("/home/user/file.txt");
assertEquals(parsedPosix.root, "/");
assertEquals(parsedPosix.dir, "/home/user");
assertEquals(parsedPosix.base, "file.txt");
assertEquals(parsedPosix.ext, ".txt");
assertEquals(parsedPosix.name, "file");
// Format path from components (POSIX)
assertEquals(
path.format({ dir: "/home/user", base: "file.txt" }),
"/home/user/file.txt"
);
}
URL Conversion
import * as path from "@std/path";
import { assertEquals } from "@std/assert";
// Convert between file URLs and paths
if (Deno.build.os === "windows") {
assertEquals(path.fromFileUrl("file:///C:/Users/user/file.txt"), "C:\\Users\\user\\file.txt");
assertEquals(path.toFileUrl("C:\\Users\\user\\file.txt").href, "file:///C:/Users/user/file.txt");
} else {
assertEquals(path.fromFileUrl("file:///home/user/file.txt"), "/home/user/file.txt");
assertEquals(path.toFileUrl("/home/user/file.txt").href, "file:///home/user/file.txt");
}
Path Properties
import * as path from "@std/path";
import { assertEquals } from "@std/assert";
// Check if path is absolute
if (Deno.build.os === "windows") {
assertEquals(path.isAbsolute("C:\\Users"), true);
assertEquals(path.isAbsolute("\\\\Server\\share"), true);
assertEquals(path.isAbsolute("C:relative\\path"), false);
assertEquals(path.isAbsolute("..\\relative\\path"), false);
} else {
assertEquals(path.isAbsolute("/home/user"), true);
assertEquals(path.isAbsolute("./relative/path"), false);
assertEquals(path.isAbsolute("../relative/path"), false);
}
// Convert to namespaced path (Windows-specific)
if (Deno.build.os === "windows") {
assertEquals(path.toNamespacedPath("C:\\Users\\file.txt"), "\\\\?\\C:\\Users\\file.txt");
assertEquals(path.toNamespacedPath("\\\\server\\share\\file.txt"), "\\\\?\\UNC\\server\\share\\file.txt");
} else {
// On POSIX, toNamespacedPath returns the path unchanged
assertEquals(path.toNamespacedPath("/home/user/file.txt"), "/home/user/file.txt");
}
Glob Pattern Utilities
import * as path from "@std/path";
import { assertEquals } from "@std/assert";
// Check if a string is a glob pattern
assertEquals(path.isGlob("*.txt"), true);
// Convert glob pattern to RegExp
const pattern = path.globToRegExp("*.txt");
assertEquals(pattern.test("file.txt"), true);
// Join multiple glob patterns
if (Deno.build.os === "windows") {
assertEquals(path.joinGlobs(["src", "**\\*.ts"]), "src\\**\\*.ts");
} else {
assertEquals(path.joinGlobs(["src", "**\/*.ts"]), "src/**\/*.ts");
}
// Normalize a glob pattern
if (Deno.build.os === "windows") {
assertEquals(path.normalizeGlob("src\\..\\**\\*.ts"), "**\\*.ts");
} else {
assertEquals(path.normalizeGlob("src/../**\/*.ts"), "**\/*.ts");
}
For POSIX-specific functions:
import { fromFileUrl } from "@std/path/posix/from-file-url";
import { assertEquals } from "@std/assert";
assertEquals(fromFileUrl("file:///home/foo"), "/home/foo");
For Windows-specific functions:
import { fromFileUrl } from "@std/path/windows/from-file-url";
import { assertEquals } from "@std/assert";
assertEquals(fromFileUrl("file:///home/foo"), "\\home\\foo");
Functions for working with URLs can be found in @std/path/posix.
Add to your project Jump to heading
deno add jsr:@std/path
See all symbols in @std/path on
何时使用 @std/path Jump to heading
任何构建、规范化或检查文件路径的场合都适用。它处理 POSIX 和 Windows 之间的差异,使你的代码可移植。
示例 Jump to heading
import { basename, dirname, extname, join, resolve } from "@std/path";
const file = join("content", "posts", "hello.md");
console.log(dirname(file)); // content/posts
console.log(basename(file)); // hello.md
console.log(extname(file)); // .md
console.log(resolve(".", "assets")); // 绝对路径
提示 Jump to heading
- 优先使用
join而非字符串拼接。 - 在文件 URL 和路径之间转换时使用
fromFileUrl/toFileUrl。 - 针对操作系统逻辑,导入
@std/path/posix或@std/path/windows。