Node: Invalid leading slash in url.parse(fileUrl).pathname on Windows

Created on 7 Nov 2018  路  7Comments  路  Source: nodejs/node

  • Version: v10.13.0
  • Platform: Windows 10 v1809
  • Subsystem:

url.parse produces incorrect pathname for file:///-based URLs under Windows, it adds a leading slash which breaks other filesystem APIs like createWriteStream(pathname). path.normalize doesn't fix it.

Example:

var url = require("url");
var path = require("path");
var fileUrl = "file:///c:/Users/avo/test.txt"
var parsed = url.parse(fileUrl)
console.log(parsed.pathname)
console.log(path.normalize(parsed.pathname))

Result:

/c:/Users/avo/test.txt
\c:\Users\avo\test.txt

A workaround I'm currently using: pathname = pathname.replace(/^\\(\w:)/, "$1");

Originally posted as a question on StackOverflow.

known limitation url windows

Most helpful comment

@vsemozhetbyt, url.fileURLToPath(url) is exactly what I needed. Spasibo!

All 7 comments

If it is appropriate, you can use url.fileURLToPath(url) now.

@vsemozhetbyt, url.fileURLToPath(url) is exactly what I needed. Spasibo!

@avoio does this solves your issue?

@bzoz yes fileURLToPath(url) did solve my issue. This bug report is however about url.parse (or perhaps, about path.normalize - should it be removing the leading \ on Windows?)

/ping @nodejs/platform-windows @nodejs/url Is this a bug or expected behavior?

I'd say expected behavior.

Hello @avoio, and thank you for the report.
AFAICT this is expected behavior:

  1. Interacting with File URLs on Windows is defined in Appendix E of rfc8089 as Nonstandard Syntax Variations
  2. \c:\Users\avo\test.txt is a valid file path which means "An alternate stream named \Users\avo\test.txt on a file named c in the current working drive".

I'm going to close this issue as a known-limitation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sandeepks1 picture sandeepks1  路  3Comments

akdor1154 picture akdor1154  路  3Comments

addaleax picture addaleax  路  3Comments

mcollina picture mcollina  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments