deno-path-from-file-url
Convert file URLs to paths.
The functionality is bundled from @std/path.
Install
yarn add deno-path-from-file-url
Usage
import process from 'node:process'
import assert from 'node:assert/strict'
import fromFileUrl from 'deno-path-from-file-url'
if (process.platform === 'win32') {
assert.equal(fromFileUrl('file:///home/foo'), String.raw`\home\foo`)
assert.equal(fromFileUrl('file:///C:/Users/foo'), String.raw`C:\Users\foo`)
assert.equal(fromFileUrl('file://localhost/home/foo'), String.raw`\home\foo`)
} else {
assert.equal(fromFileUrl('file:///home/foo'), '/home/foo')
}
For POSIX-specific functions:
import assert from 'node:assert/strict'
import fromFileUrl from 'deno-path-from-file-url/posix'
assert.equal(fromFileUrl('file:///home/foo'), '/home/foo')
For Windows-specific functions:
import assert from 'node:assert/strict'
import fromFileUrl from 'deno-path-from-file-url/windows'
assert.equal(fromFileUrl('file:///home/foo'), String.raw`\home\foo`)
assert.equal(fromFileUrl('file:///C:/Users/foo'), String.raw`C:\Users\foo`)
assert.equal(fromFileUrl('file://localhost/home/foo'), String.raw`\home\foo`)
Motivation
Prettier need this functionality, can't use node:url.fileURLToPath() since it runs in both Node.js and browsers, can't install @std/path either since Prettier supports install from GitHub and not all NPM clients support jsr: protocol.