This function ensures that path is resolved absolutely, and that the URL control characters are correctly encoded when converting into a File URL.
path
import { pathToFileURL } from 'node:url';new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.cpathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) Copy
import { pathToFileURL } from 'node:url';new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.cpathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
The path to convert to a File URL.
Optional
The file URL object.
v10.12.0
This function ensures that
path
is resolved absolutely, and that the URL control characters are correctly encoded when converting into a File URL.