4

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?)

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

Note that posix doesn't solve all your problems. For example, when run from D:\\mydir: path.resolve('myfile') (D:\\mydir\\myfile) path.posix.resolve('myfile') (D:\\mydir/myfile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.