问题描述
是否存在纠正从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.
有解决方案吗?
推荐答案
请务必使用方法,而不是输入路径。 和是在开发跨平台时特别有用:
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 —正斜杠,反斜杠纠正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!