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

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)

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

Given that all the other answers rely on installing (either way too large, or way too small) 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 already) using Node's standard library path module, and more specifically, taking advantage of its dedicated path.posix and path.win32 namespaced properties/functions (introduced in Node v0.11):

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 a POSIX compliant platform, or on Windows, without needing any kind of external dependency.


For those looking for an answer that doesn't depend on Node.js

One Liner (no 3rd party library)

//
// 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,'\/')

Normally I import libraries. However, I went and read the source code for both slash and upath. The functions were not particularly up to date, and incredibly small at the time I checked. In fact, this one liner actually handles more cases than the slash library. Not everyone is looking for this kind of solution, but for those that are, here it is. By coincidence this has the fastest runtime of all the current answers.