javascript - How to correctly create cross platform paths with Nodejs? - Stack Overflow

I have a project and I build path from token strings using path join e.g.let myPath = path.join('r

I have a project and I build path from token strings using path join e.g.

 let myPath = path.join('root', 'path01')

Normally I develop on a POSIX dev machine so no issues at all... but (Unexpected) is that if I run the app on windows it will build paths backslashed

And finally the app is deployed on a POSIX system so it will produce possibly wrong expectations (if starting to develop from windows)...

What's the best way to treat paths with nodejs/javascript so to have them working on all platforms?

I have a project and I build path from token strings using path join e.g.

 let myPath = path.join('root', 'path01')

Normally I develop on a POSIX dev machine so no issues at all... but (Unexpected) is that if I run the app on windows it will build paths backslashed

And finally the app is deployed on a POSIX system so it will produce possibly wrong expectations (if starting to develop from windows)...

What's the best way to treat paths with nodejs/javascript so to have them working on all platforms?

Share Improve this question asked Feb 4, 2021 at 8:57 koalaokkoalaok 5,76012 gold badges61 silver badges96 bronze badges 1
  • One question to consider is whether users on different platforms may work on the same set of paths in some way. If not, path is probably best. OTOH if there are for example collaboration features, you may want to use a single normalized representation of paths internally (e.g., POSIX even on Windows) and convert them to platform-specific format when actually interacting with the filesystem. – Anton Strogonoff Commented Oct 16, 2023 at 7:42
Add a ment  | 

3 Answers 3

Reset to default 4

Use Upath (Universal Path), a drop-in replacement to Node's path library:

  • https://www.npmjs./package/upath

I've just discovered it, and it's brilliant.

What's the best way to treat paths with nodejs/javascript so to have them working on all platforms?

You are already using the best way, which is by using the path module functionality.

Normally I develop on a POSIX dev machine so no issues at all... but (Unexpected) is that if I run the app on windows it will build paths backslashed

This should be expected, because that's the whole purpose of the using path.join. When your code runs, path.join will join your path using the correct delimiter for the operating system, i.e. / on POSIX and \ on Windows.

And finally the app is deployed on a POSIX system so it will produce possibly wrong expectations (if starting to develop from windows)...

There won't be an issue, since you are running on POSIX, path.join will create a path suitable for your POSIX system.

You've got a few friends that will help, I'd start off by looking at the Node path object and its methods. path.delimiter in specific to start with, but there may be some short-cut methods that can help you as well.

https://nodejs/api/path.html#path_path_delimiter

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744852725a4597224.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信