Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 356 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I convert a windows path to posix path using node path

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

My goal is to convert `C:\repos\vue-t\tests\views\index\home.vue` to `C:/repos/vue-t/tests/views/index/home.vue`

so I can use it in an import on a file I'm writing to the disk

const appImport = `
import Vue from "vue"
import App from '${path}'

function createApp (data) {
const app = new Vue({
data,
render: h => h(App)
})
return app
}`

//this string is then written to the disk as a file

I'd prefer not to `.replace(/\\/g, '/')` the string, and would rather prefer to use a `require('path')` function.
Reply

#2
[Slash][1] converts windows backslash paths to Unix paths


[1]:

[To see links please register here]


**Usage:**

const path = require('path');
const slash = require('slash');

const str = path.join('foo', 'bar');

slash(str);
// Unix => foo/bar
// Windows => foo/bar
Reply

#3
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)
Reply

#4
<!-- language: lang-js -->

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'

<!-- end snippet -->
Reply

#5
Just use default lib as:

const {direname, resolve, basename}= require('path').posix;

or

import {posix} from 'path';
const {direname, resolve, basename}= posix;

Reply

#6
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:

[To see links please register here]

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.
Reply

#7
I was looking for something similar, but a little more universal especially to include drives in the absolute path. Thus if you work with e.g. git-bash or WSL usually drives are mapped by default as letters from root / (git-bash) or /mnt (WSL). So here is a regex that does the job for me

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// For git-bash Windows drives are mounted in the root like /C/ /D/ etc.
const toGitBashPosixPath = (windowsPath) => windowsPath.replace(/^(\w):|\\+/g,'/$1');

console.log(toGitBashPosixPath('c:\\\\\\project\\file.x')); // messy Windows path
console.log(toGitBashPosixPath('c:\\project\\file.x')); // regular Windows path
console.log(toGitBashPosixPath('c:/project/file.x')); // slash path acceptable by Windows
console.log(toGitBashPosixPath('project\\file.x'));// relative Windows path
console.log(toGitBashPosixPath('.\\project\\file.x'));// another relative Windows path

// For WSL Windows drives are mounted by default next to /mnt like /mnt/C/ /mnt/D/ etc.
const toWSLPosixPath = (windowsPath) => windowsPath.replace(/^(\w):|\\+/g,'/$1').replace(/^\//g,'/mnt/');
console.log(toWSLPosixPath('c:\\project\\file.x'))

<!-- end snippet -->

Hopefully this will help someone.
Reply

#8
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 anyway) using Node's standard library [`path`](

[To see links please register here]

) module, and more specifically, taking advantage of its dedicated [`path.posix`](

[To see links please register here]

) and [`path.win32`](

[To see links please register here]

) namespaced properties/functions (introduced all the way back in Node v0.11):

```js
import path from "path"; // or in legacy cjs: const path = require("path")

// Split on whatever is "this OS's path separator",
// then explicitly join with posix or windows separators:

const definitelyPosix = somePathString.split(path.sep).join(path.posix.sep);
const definitelyWindows = somePathString.split(path.sep).join(path.win32.sep);
```

This will convert your path to POSIX (or Windows) format irrespective of whether you're already on a POSIX (or Windows) compliant platform, without needing any kind of external dependency.

Or, if you don't even care about which OS you're in:

```js
import path from "path";

const { platform } = process;
const locale = path[platform === `win32` ? `win32` : `posix`];

...

const localePath = somepath.split(path.sep).join(locale.sep);
```

And of course, be aware that paths with `/` in them _have always been valid paths in Windows_, ever since the very first windows 1.0 days. So there's not a lot of value in turning Posix paths into Windows paths (manually, at least. If you need absolute paths, Posix and Windows differ drastically in how those are exposed, of course, but pretty much any time you need an absolute path, that's a config value and you should be using `.env` files anyway)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through