URL

pathToFileURL()

Converts a filesystem path into an absolute file URL, escaping URL-significant characters. Tested on Node.js 22.16.0. Use this for path-to-URL conversion rather than concatenating a file scheme with raw path text.

Formal Syntax & Structure

nodejs syntaxnodejs
pathToFileURL(path[, options])

Parameters & Attributes

NameTypeDescription
pathstringFilesystem path.
optionsobjectOptional windows selector (default platform).

Return Value

URL

File URL object.

Working Example

Example Snippetnodejs
import { URL, URLSearchParams, fileURLToPath, pathToFileURL } from 'node:url';console.log(pathToFileURL('/catalog/book#1.txt',{windows:false}).href);
Node.js Reference