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

前端 未结 5 865
心在旅途
心在旅途 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-03 23:39

    A one-liner mimicking slash and upath (see the multi-liner version for proof of validity)

    //
    // one-liner
    //
    let convertPath = (windowsPath) => windowsPath.replace(/^\\\\\?\\/,"").replace(/\\/g,'\/').replace(/\/\/+/g,'\/')
    
    //
    // usage
    //
    convertPath("C:\\repos\\vue-t\\tests\\views\\index\\home.vue")
    // >>> "C:/repos/vue-t/tests/views/index/home.vue"
    
    //
    // multi-liner (commented and compatible with really old javascript versions)
    //
    function convertPath(windowsPath) {
        // handle the edge-case of Window's long file names
        // See: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names
        windowsPath = windowsPath.replace(/^\\\\\?\\/,"");
    
        // convert the separators, valid since both \ and / can't be in a windows filename
        windowsPath = windowsPath.replace(/\\/g,'\/');
    
        // compress any // or /// to be just /, which is a safe oper under POSIX
        // and prevents accidental errors caused by manually doing path1+path2
        windowsPath = windowsPath.replace(/\/\/+/g,'\/');
    
        return windowsPath;
    };
    
    // dont want the C: to be inluded? here's a one-liner for that too
    let convertPath = (windowsPath) => windowsPath.replace(/^\\\\\?\\/,"").replace(/(?:^C:)?\\/g,'\/').replace(/\/\/+/g,'\/')
    
    0 讨论(0)
  • 2021-01-03 23:41
    const winPath = 'C:\\repos\\vue-t\\tests\\views\\index\\home.vue'
    const posixPath = winPath.replace(/\\/g, '/').slice(2)
    // Now posixPath = '/repos/vue-t/tests/views/index/home.vue'
    
    0 讨论(0)
  • 2021-01-03 23:45

    Slash converts windows backslash paths to Unix paths

    Usage:

    const path = require('path');
    const slash = require('slash');
    
    const str = path.join('foo', 'bar');
    
    slash(str);
    // Unix    => foo/bar
    // Windows => foo/bar
    
    0 讨论(0)
  • 2021-01-04 00:00

    There is node package called upath will convert windows path into unix.

    upath = require('upath');
    

    or

    import * as upath from 'upath';
    
    upath.toUnix(destination_path)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题