如何使用节点路径将Windows路径转换为posix路径

如何使用节点路径将Windows路径转换为posix路径

问题描述:

我正在Windows上进行开发,但是需要知道如何将Windows路径(带有反斜杠\)转换为带有正斜杠(/)的POSIX路径吗?

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

我的目标是将C:\repos\vue-t\tests\views\index\home.vue转换为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

我不希望不使用.replace(/\\/g, '/')字符串,而是希望使用require('path')函数.

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

斜杠将Windows反斜杠路径转换为Unix路径

Slash converts windows backslash paths to Unix paths

用法:

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

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

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