Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 729
indubitablee Avatar asked Mar 12 '13 15:03

indubitablee


2 Answers

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.

like image 101
Chad Avatar answered Nov 15 '22 05:11

Chad


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);
like image 29
Jan Święcki Avatar answered Nov 15 '22 05:11

Jan Święcki