Node.js: Check the root of the path.format()

See the following example for the path.format() :

require('path').format({ root: '/Users/joe', name: 'test', ext: 'txt' }) //  '/Users/joe/test.txt'

This example is written in and it is said that the output is as follows:


But I have tested this code on both Linux and Windows, but the result is as follows:


What is the reason for this difference?
Is the example of this site wrong?

Hi @GrH, I’m not sure but the problem might be that you’re already assuming a POSIX path in the root string, and path.format() doesn’t know how to handle this… try using path.join('Users', 'joe') to apply the correct delimiter on both platforms. Also, shouldn’t that actually be dir and not root here anyway?

PS: Just had a look at the link, and they’re indeed saying:

require('path').format({ dir: 'C:\\Users\\joe', base: 'test.txt' }) //  'C:\\Users\\joe\\test.txt'
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.