How can I convert a windows path to posix path using node path

前端 未结 5 864
心在旅途
心在旅途 2021-01-03 23:28

I\'m developing on windows, but need to know how to convert a windows path (with backslashes \\) into a POSIX path with forward slashes (/)?

<
5条回答
  •  一生所求
    2021-01-04 00:01

    Given that all the other answers rely on installing (large) third party modules: this can also be done as a one-liner for relative paths (which you should be using 99.999% of the time) using Node's standard library path module, and more specifically, taking advantage of its dedicated path.posix and path.win32 namespaced properties/functions:

    const path = require("path");
    
    // ...
    
    const definitelyPosix = somePathString.split(path.sep).join(path.posix.sep);
    

    This will convert your path to POSIX format irrespective of whether you're already on POSIX platforms, or on win32, while requiring zero dependencies.

提交回复
热议问题