如何使用节点路径将Windows路径转换为posix路径 [英] How can I convert a windows path to posix path using node path
本文介绍了如何使用节点路径将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
这篇关于如何使用节点路径将Windows路径转换为posix路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文