Node.js for Windows and Macs — forward slash, backslash rectification

蹲街弑〆低调 提交于 2020-01-13 11:43:31

问题


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

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.

Is there a solution to this?


回答1:


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

On Windows:

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

On Linux:

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

Hope this helps.




回答2:


In addition to Chad answer, when you are constructing paths you can:

var path = require("path");
"hey" + path.sep + "there" + path.sep + "you"

or

["hey", "there", "you"].join(path.sep);


来源:https://stackoverflow.com/questions/15364823/node-js-for-windows-and-macs-forward-slash-backslash-rectification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!