首页 > 解决方案 > node.js pathToFileURL()/fileURLToPath() 中是否有错误?

问题描述

我尝试了以下单元测试:

test('nodejs URL pathToFileURL() works', () => {
    let dosPath = '\\\\?\\UNC\\diskstation\\home\\cpan-modules.txt'
    let theUrl = pathToFileURL(dosPath);
    let thePath = fileURLToPath(theUrl);
})
;

它失败了,但有以下异常:

TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute
    at getPathFromURLWin32 (internal/url.js:1310:13)
    at Object.fileURLToPath (internal/url.js:1342:22)
    at Context.<anonymous> (c:\<anonymized>\out\test\suite\extension.test.js:85:29)
    at processImmediate (internal/timers.js:439:21)

该文件存在并且可以读取:

图像显示命令 <code>type \\?\UNC\diskstation\home\cpan-modules.txt</code>

请注意,文档说这pathToFileURL()将使 path absolute. 这使得fileUrlToPath()关于路径不是绝对的抱怨非常奇怪。

我正在使用 VS-Code 1.43.1 开发一个 VS-Code 扩展

显示 VS-Code 版本的图像

标签: node.jswindowsntfsunc

解决方案


推荐阅读