Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js Path modules: use Unix path separators even when running on Windows

I have a script, which is using standard node.js path modules https://nodejs.org/api/path.html

When using path functions such as 'path.relative', the path library looks at what operating system I'm on, and uses that OS's path separator. (\ on Windows, / on POSIX). That's cool, and makes a lot of tasks very convenient. The problem is, the script is auto-generating some files which are intended for a UNIX platform, and need the Unix path separator. But sometimes the script will be being run from a Windows platform. I need the output of the script to be the same (Unix path separators), regardless of which platform is running the script. Is there a way to do this? (To make path use a specific path separator, rather than using the one for the operating system running Node?)

like image 408
ffConundrums Avatar asked Oct 23 '25 08:10

ffConundrums


1 Answers

You should be able to use path.posix instead of just path.

Example:

var path = require('path');
path.join('a', 'b'); // a\\b
path.posix.join('a', 'b'); // a/b
like image 174
jas7457 Avatar answered Oct 24 '25 22:10

jas7457