适用于Windows和Mac的Node.js —正斜杠,反斜杠纠正 [英] Node.js for Windows and Macs — forward slash, backslash rectification

查看:383
本文介绍了适用于Windows和Mac的Node.js —正斜杠,反斜杠纠正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在纠正从Windows到Linux和Mac的node.js中关于反斜杠与正斜杠的差异的方法?

Is there a method to rectify the discrepancy in node.js from Windows to Linux and Mac concerning the backslash versus forward slash?

Windows在调用Windows中的位置时需要反斜杠 git bash ,而Mac / Linux需要使用正斜杠。我正在与Mac和Windows用户同时进行项目,因此我无法将代码中的所有正斜杠更改为反斜杠,因为当Mac用户拉动时,coffee将无法为他们正确运行,反之亦然。

Windows requires backslashes when calling locations in git bash, while Mac/Linux requires forward slashes. I'm working on a project with both Mac and Windows users so I can't change all the forward slashes to backslashes in the code because when Mac users pull, coffee wont be able to properly run for them and vice versa.

有解决方案吗?

推荐答案

请务必使用路径方法,而不是输入路径。 path.normalize() path.join() 是在开发跨平台时特别有用:

Make sure to use path methods instead of typing out paths. path.normalize() and path.join() are particularly useful when developing cross platform:

在Windows上:

$ node
> var p = require('path')
undefined
> p.normalize('/hey/there/you')
'\\hey\\there\\you'
> p.join('/hey', 'there', '/you')
'\\hey\\there\\you'

在Linux上:

$ node
> var p = require('path')
undefined
> p.normalize('/hey/there/you')
'/hey/there/you'
> p.join('/hey', 'there', '/you')
'/hey/there/you'

希望这会有所帮助。

这篇关于适用于Windows和Mac的Node.js —正斜杠,反斜杠纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆